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"
117 " -f file read operations from `file'\n"
118 " -h host LDAP server\n"
119 " -H URI LDAP Uniform Resource Indentifier(s)\n"
120 " -I use SASL Interactive mode\n"
121 " -k use Kerberos authentication\n"
122 " -K like -k, but do only step 1 of the Kerberos bind\n"
123 " -M enable Manage DSA IT control (-MM to make critical)\n"
124 " -n show what would be done but don't actually update\n"
125 " -O props SASL security properties\n"
126 " -p port port on LDAP server\n"
127 " -P version procotol version (default: 3)\n"
128 " -Q use SASL Quiet mode\n"
129 " -R realm SASL realm\n"
130 " -U authcid SASL authentication identity\n"
131 " -v run in verbose mode (diagnostics to standard output)\n"
132 " -w passwd bind passwd (for simple authentication)\n"
133 " -W prompt for bind passwd\n"
134 " -x Simple authentication\n"
135 " -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
136 " -y file Read passwd from file\n"
137 " -Y mech SASL mechanism\n"
138 " -Z Start TLS request (-ZZ to require successful response)\n"
139 , prog, (strcmp( prog, "ldapadd" ) ? " is to replace" : "") );
141 exit( EXIT_FAILURE );
146 main( int argc, char **argv )
148 char *infile, *rejfile, *rbuf, *start, *rejbuf = NULL;
150 char *matched_msg = NULL, *error_msg = NULL;
151 int rc, i, authmethod, version, want_bindpw, debug, manageDSAit, noop, referrals;
153 char *pw_file = NULL;
154 char *control, *cvalue;
157 prog = lutil_progname( "ldapmodify", argc, argv );
159 /* Print usage when no parameters */
160 if( argc < 2 ) usage( prog );
162 /* strncmp instead of strcmp since NT binaries carry .exe extension */
163 ldapadd = ( strncmp( prog, "ldapadd", sizeof("ldapadd")-1 ) == 0 );
167 not = verbose = want_bindpw = debug = manageDSAit = noop = referrals = 0;
171 while (( i = getopt( argc, argv, "acrf:E:F"
172 "Cd:D:e:h:H:IkKMnO:p:P:QR:S:U:vw:WxX:y:Y:Z" )) != EOF )
179 case 'c': /* continuous operation */
182 case 'E': /* modify controls */
183 if( version == LDAP_VERSION2 ) {
184 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
189 /* should be extended to support comma separated list of
190 * [!]key[=value] parameters, e.g. -E !foo,bar=567
195 if( optarg[0] == '!' ) {
200 control = strdup( optarg );
201 if ( (cvalue = strchr( control, '=' )) != NULL ) {
204 fprintf( stderr, "Invalid modify control name: %s\n", control );
207 case 'f': /* read from file */
208 if( infile != NULL ) {
209 fprintf( stderr, "%s: -f previously specified\n", prog );
212 infile = strdup( optarg );
214 case 'F': /* force all changes records to be used */
223 debug |= atoi( optarg );
225 case 'D': /* bind DN */
226 if( binddn != NULL ) {
227 fprintf( stderr, "%s: -D previously specified\n", prog );
230 binddn = strdup( optarg );
232 case 'e': /* general controls */
233 if( version == LDAP_VERSION2 ) {
234 fprintf( stderr, "%s: -e incompatible with LDAPv%d\n",
239 /* should be extended to support comma separated list of
240 * [!]key[=value] parameters, e.g. -e !foo,bar=567
245 if( optarg[0] == '!' ) {
250 control = strdup( optarg );
251 if ( (cvalue = strchr( control, '=' )) != NULL ) {
255 if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
257 fprintf( stderr, "manageDSAit control previously specified");
260 if( cvalue != NULL ) {
261 fprintf( stderr, "manageDSAit: no control value expected" );
266 manageDSAit = 1 + crit;
270 } else if ( strcasecmp( control, "noop" ) == 0 ) {
272 fprintf( stderr, "noop control previously specified");
275 if( cvalue != NULL ) {
276 fprintf( stderr, "noop: no control value expected" );
286 fprintf( stderr, "Invalid general control name: %s\n", control );
290 case 'h': /* ldap host */
291 if( ldapuri != NULL ) {
292 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
295 if( ldaphost != NULL ) {
296 fprintf( stderr, "%s: -h previously specified\n", prog );
299 ldaphost = strdup( optarg );
301 case 'H': /* ldap URI */
302 if( ldaphost != NULL ) {
303 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
307 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
310 if( ldapuri != NULL ) {
311 fprintf( stderr, "%s: -H previously specified\n", prog );
314 ldapuri = strdup( optarg );
317 #ifdef HAVE_CYRUS_SASL
318 if( version == LDAP_VERSION2 ) {
319 fprintf( stderr, "%s: -I incompatible with version %d\n",
323 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
324 fprintf( stderr, "%s: incompatible previous "
325 "authentication choice\n",
329 authmethod = LDAP_AUTH_SASL;
330 version = LDAP_VERSION3;
331 sasl_flags = LDAP_SASL_INTERACTIVE;
334 fprintf( stderr, "%s: was not compiled with SASL support\n",
336 return( EXIT_FAILURE );
338 case 'k': /* kerberos bind */
339 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
340 if( version > LDAP_VERSION2 ) {
341 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
346 if( authmethod != -1 ) {
347 fprintf( stderr, "%s: -k incompatible with previous "
348 "authentication choice\n", prog );
352 authmethod = LDAP_AUTH_KRBV4;
354 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
358 case 'K': /* kerberos bind, part one only */
359 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
360 if( version > LDAP_VERSION2 ) {
361 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
365 if( authmethod != -1 ) {
366 fprintf( stderr, "%s: incompatible with previous "
367 "authentication choice\n", prog );
371 authmethod = LDAP_AUTH_KRBV41;
373 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
374 return( EXIT_FAILURE );
378 /* enable Manage DSA IT */
379 if( version == LDAP_VERSION2 ) {
380 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
385 version = LDAP_VERSION3;
387 case 'n': /* print deletes, don't actually do them */
391 #ifdef HAVE_CYRUS_SASL
392 if( sasl_secprops != NULL ) {
393 fprintf( stderr, "%s: -O previously specified\n", prog );
396 if( version == LDAP_VERSION2 ) {
397 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
401 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
402 fprintf( stderr, "%s: incompatible previous "
403 "authentication choice\n", prog );
406 authmethod = LDAP_AUTH_SASL;
407 version = LDAP_VERSION3;
408 sasl_secprops = strdup( optarg );
410 fprintf( stderr, "%s: not compiled with SASL support\n",
412 return( EXIT_FAILURE );
417 fprintf( stderr, "%s: -p previously specified\n", prog );
420 ldapport = atoi( optarg );
423 switch( atoi(optarg) ) {
425 if( version == LDAP_VERSION3 ) {
426 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
430 version = LDAP_VERSION2;
433 if( version == LDAP_VERSION2 ) {
434 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
438 version = LDAP_VERSION3;
441 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
444 return( EXIT_FAILURE );
447 #ifdef HAVE_CYRUS_SASL
448 if( version == LDAP_VERSION2 ) {
449 fprintf( stderr, "%s: -Q incompatible with version %d\n",
453 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
454 fprintf( stderr, "%s: incompatible previous "
455 "authentication choice\n",
459 authmethod = LDAP_AUTH_SASL;
460 version = LDAP_VERSION3;
461 sasl_flags = LDAP_SASL_QUIET;
464 fprintf( stderr, "%s: not compiled with SASL support\n",
466 return( EXIT_FAILURE );
468 case 'r': /* replace (obsolete) */
472 #ifdef HAVE_CYRUS_SASL
473 if( sasl_realm != NULL ) {
474 fprintf( stderr, "%s: -R previously specified\n", prog );
477 if( version == LDAP_VERSION2 ) {
478 fprintf( stderr, "%s: -R incompatible with version %d\n",
482 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
483 fprintf( stderr, "%s: incompatible previous "
484 "authentication choice\n",
488 authmethod = LDAP_AUTH_SASL;
489 version = LDAP_VERSION3;
490 sasl_realm = strdup( optarg );
492 fprintf( stderr, "%s: not compiled with SASL support\n",
494 return( EXIT_FAILURE );
497 case 'S': /* skipped modifications to file */
498 if( rejfile != NULL ) {
499 fprintf( stderr, "%s: -S previously specified\n", prog );
502 rejfile = strdup( optarg );
505 #ifdef HAVE_CYRUS_SASL
506 if( sasl_authc_id != NULL ) {
507 fprintf( stderr, "%s: -U previously specified\n", prog );
510 if( version == LDAP_VERSION2 ) {
511 fprintf( stderr, "%s: -U incompatible with version %d\n",
515 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
516 fprintf( stderr, "%s: incompatible previous "
517 "authentication choice\n",
521 authmethod = LDAP_AUTH_SASL;
522 version = LDAP_VERSION3;
523 sasl_authc_id = strdup( optarg );
525 fprintf( stderr, "%s: not compiled with SASL support\n",
527 return( EXIT_FAILURE );
530 case 'v': /* verbose mode */
533 case 'w': /* password */
534 passwd.bv_val = strdup( optarg );
538 for( p = optarg; *p != '\0'; p++ ) {
542 passwd.bv_len = strlen( passwd.bv_val );
551 #ifdef HAVE_CYRUS_SASL
552 if( sasl_mech != NULL ) {
553 fprintf( stderr, "%s: -Y previously specified\n", prog );
556 if( version == LDAP_VERSION2 ) {
557 fprintf( stderr, "%s: -Y incompatible with version %d\n",
561 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
562 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
565 authmethod = LDAP_AUTH_SASL;
566 version = LDAP_VERSION3;
567 sasl_mech = strdup( optarg );
569 fprintf( stderr, "%s: not compiled with SASL support\n",
571 return( EXIT_FAILURE );
575 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
576 fprintf( stderr, "%s: incompatible with previous "
577 "authentication choice\n", prog );
580 authmethod = LDAP_AUTH_SIMPLE;
583 #ifdef HAVE_CYRUS_SASL
584 if( sasl_authz_id != NULL ) {
585 fprintf( stderr, "%s: -X previously specified\n", prog );
588 if( version == LDAP_VERSION2 ) {
589 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
593 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
594 fprintf( stderr, "%s: -X incompatible with "
595 "authentication choice\n", prog );
598 authmethod = LDAP_AUTH_SASL;
599 version = LDAP_VERSION3;
600 sasl_authz_id = strdup( optarg );
602 fprintf( stderr, "%s: not compiled with SASL support\n",
604 return( EXIT_FAILURE );
609 if( version == LDAP_VERSION2 ) {
610 fprintf( stderr, "%s: -Z incompatible with version %d\n",
614 version = LDAP_VERSION3;
617 fprintf( stderr, "%s: not compiled with TLS support\n",
619 return( EXIT_FAILURE );
623 fprintf( stderr, "%s: unrecognized option -%c\n",
630 version = LDAP_VERSION3;
632 if (authmethod == -1 && version > LDAP_VERSION2) {
633 #ifdef HAVE_CYRUS_SASL
634 authmethod = LDAP_AUTH_SASL;
636 authmethod = LDAP_AUTH_SIMPLE;
640 if ( argc != optind )
643 if ( rejfile != NULL ) {
644 if (( rejfp = fopen( rejfile, "w" )) == NULL ) {
646 return( EXIT_FAILURE );
652 if ( infile != NULL ) {
653 if (( fp = fopen( infile, "r" )) == NULL ) {
655 return( EXIT_FAILURE );
662 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
663 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
665 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
666 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
672 (void) SIGNAL( SIGPIPE, SIG_IGN );
676 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
678 fprintf( stderr, "ldap_init( %s, %d )\n",
679 ldaphost != NULL ? ldaphost : "<DEFAULT>",
683 ld = ldap_init( ldaphost, ldapport );
685 perror("ldapmodify: ldap_init");
691 fprintf( stderr, "ldap_initialize( %s )\n",
692 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
695 rc = ldap_initialize( &ld, ldapuri );
696 if( rc != LDAP_SUCCESS ) {
697 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
698 rc, ldap_err2string(rc) );
704 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
705 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
707 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
708 referrals ? "on" : "off" );
713 if (version == -1 ) {
714 version = LDAP_VERSION3;
717 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
718 != LDAP_OPT_SUCCESS )
720 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
725 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
726 ldap_perror( ld, "ldap_start_tls" );
728 return( EXIT_FAILURE );
732 if ( pw_file || want_bindpw ) {
734 rc = lutil_get_filed_password( pw_file, &passwd );
735 if( rc ) return EXIT_FAILURE;
737 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
738 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
742 if ( authmethod == LDAP_AUTH_SASL ) {
743 #ifdef HAVE_CYRUS_SASL
746 if( sasl_secprops != NULL ) {
747 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
748 (void *) sasl_secprops );
750 if( rc != LDAP_OPT_SUCCESS ) {
752 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
754 return( EXIT_FAILURE );
758 defaults = lutil_sasl_defaults( ld,
765 rc = ldap_sasl_interactive_bind_s( ld, binddn,
766 sasl_mech, NULL, NULL,
767 sasl_flags, lutil_sasl_interact, defaults );
769 if( rc != LDAP_SUCCESS ) {
770 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
771 return( EXIT_FAILURE );
774 fprintf( stderr, "%s: not compiled with SASL support\n",
776 return( EXIT_FAILURE );
780 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
782 ldap_perror( ld, "ldap_bind" );
783 return( EXIT_FAILURE );
792 if ( manageDSAit || noop ) {
795 LDAPControl *ctrls[3];
800 c1.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
801 c1.ldctl_value.bv_val = NULL;
802 c1.ldctl_value.bv_len = 0;
803 c1.ldctl_iscritical = manageDSAit > 1;
810 c2.ldctl_oid = LDAP_CONTROL_NOOP;
811 c2.ldctl_value.bv_val = NULL;
812 c2.ldctl_value.bv_len = 0;
813 c2.ldctl_iscritical = noop > 1;
816 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
818 if( err != LDAP_OPT_SUCCESS ) {
819 fprintf( stderr, "Could not set %scontrols\n",
820 (c1.ldctl_iscritical || c2.ldctl_iscritical)
821 ? "critical " : "" );
822 if ( c1.ldctl_iscritical && c2.ldctl_iscritical ) {
829 while (( rc == 0 || contoper ) &&
830 ( rbuf = read_one_record( fp )) != NULL ) {
836 len = strlen( rbuf );
837 if (( rejbuf = (char *)malloc( len+1 )) == NULL ) {
839 exit( EXIT_FAILURE );
841 memcpy( rejbuf, rbuf, len+1 );
844 rc = process_ldif_rec( start, count );
847 fprintf(rejfp, "# Error: %s (%d)", ldap_err2string(rc), rc);
849 ldap_get_option(ld, LDAP_OPT_MATCHED_DN, &matched_msg);
850 if ( matched_msg != NULL && *matched_msg != '\0' ) {
851 fprintf( rejfp, ", matched DN: %s", matched_msg );
854 ldap_get_option(ld, LDAP_OPT_ERROR_STRING, &error_msg);
855 if ( error_msg != NULL && *error_msg != '\0' ) {
856 fprintf( rejfp, ", additional info: %s", error_msg );
858 fprintf( rejfp, "\n%s\n", rejbuf );
869 if ( rejfp != NULL ) {
878 process_ldif_rec( char *rbuf, int count )
880 char *line, *dn, *type, *newrdn, *newsup, *p;
881 int rc, linenum, modop, replicaport;
882 int expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
883 int expect_deleteoldrdn, deleteoldrdn;
884 int saw_replica, use_record, new_entry, delete_entry, got_all;
891 rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
892 expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
893 expect_sep = expect_ct = 0;
899 dn = newrdn = newsup = NULL;
901 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
904 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
910 if ( ldif_parse_line( line, &type, &val.bv_val, &val.bv_len ) < 0 ) {
911 fprintf( stderr, "%s: invalid format (line %d) entry: \"%s\"\n",
912 prog, linenum, dn == NULL ? "" : dn );
913 rc = LDAP_PARAM_ERROR;
918 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
920 if (( p = strchr( val.bv_val, ':' )) == NULL ) {
924 replicaport = atoi( p );
926 if ( ldaphost != NULL && strcasecmp( val.bv_val, ldaphost ) == 0 &&
927 replicaport == ldapport ) {
930 } else if ( count == 1 && linenum == 1 &&
931 strcasecmp( type, T_VERSION_STR ) == 0 )
933 if( val.bv_len == 0 || atoi(val.bv_val) != 1 ) {
934 fprintf( stderr, "%s: invalid version %s, line %d (ignored)\n",
935 prog, val.bv_val == NULL ? "(null)" : val.bv_val, linenum );
939 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
940 if (( dn = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
942 exit( EXIT_FAILURE );
946 goto end_line; /* skip all lines until we see "dn:" */
951 if ( !use_record && saw_replica ) {
952 printf( "%s: skipping change record for entry: %s\n"
953 "\t(LDAP host/port does not match replica: lines)\n",
957 ber_memfree( val.bv_val );
961 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
962 #ifdef LIBERAL_CHANGETYPE_MODOP
963 /* trim trailing spaces (and log warning ...) */
966 for ( icnt = val.bv_len; --icnt > 0; ) {
967 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
972 if ( ++icnt != val.bv_len ) {
973 fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
974 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
975 val.bv_val[icnt] = '\0';
977 #endif /* LIBERAL_CHANGETYPE_MODOP */
979 if ( strcasecmp( val.bv_val, T_MODIFYCTSTR ) == 0 ) {
982 } else if ( strcasecmp( val.bv_val, T_ADDCTSTR ) == 0 ) {
984 } else if ( strcasecmp( val.bv_val, T_MODRDNCTSTR ) == 0
985 || strcasecmp( val.bv_val, T_MODDNCTSTR ) == 0
986 || strcasecmp( val.bv_val, T_RENAMECTSTR ) == 0)
989 } else if ( strcasecmp( val.bv_val, T_DELETECTSTR ) == 0 ) {
990 got_all = delete_entry = 1;
993 "%s: unknown %s \"%s\" (line %d of entry \"%s\")\n",
994 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
995 rc = LDAP_PARAM_ERROR;
998 } else if ( ldapadd ) { /* missing changetype => add */
1000 modop = LDAP_MOD_ADD;
1002 expect_modop = 1; /* missing changetype => modify */
1006 if ( expect_modop ) {
1007 #ifdef LIBERAL_CHANGETYPE_MODOP
1008 /* trim trailing spaces (and log warning ...) */
1011 for ( icnt = val.bv_len; --icnt > 0; ) {
1012 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
1017 if ( ++icnt != val.bv_len ) {
1018 fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
1019 prog, type, val.bv_val, linenum, dn );
1020 val.bv_val[icnt] = '\0';
1022 #endif /* LIBERAL_CHANGETYPE_MODOP */
1026 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
1027 modop = LDAP_MOD_ADD;
1029 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
1030 modop = LDAP_MOD_REPLACE;
1031 addmodifyop( &pmods, modop, val.bv_val, NULL );
1033 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
1034 modop = LDAP_MOD_DELETE;
1035 addmodifyop( &pmods, modop, val.bv_val, NULL );
1037 } else { /* no modify op: use default */
1038 modop = ldapadd ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
1042 if ( expect_newrdn ) {
1043 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
1044 if (( newrdn = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
1046 exit( EXIT_FAILURE );
1048 expect_deleteoldrdn = 1;
1051 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
1052 prog, T_NEWRDNSTR, type, linenum, dn );
1053 rc = LDAP_PARAM_ERROR;
1055 } else if ( expect_deleteoldrdn ) {
1056 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
1057 deleteoldrdn = ( *val.bv_val == '0' ) ? 0 : 1;
1058 expect_deleteoldrdn = 0;
1062 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
1063 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
1064 rc = LDAP_PARAM_ERROR;
1066 } else if ( expect_newsup ) {
1067 if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
1068 if (( newsup = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
1070 exit( EXIT_FAILURE );
1074 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
1075 prog, T_NEWSUPSTR, type, linenum, dn );
1076 rc = LDAP_PARAM_ERROR;
1078 } else if ( got_all ) {
1080 "%s: extra lines at end (line %d of entry \"%s\")\n",
1081 prog, linenum, dn );
1082 rc = LDAP_PARAM_ERROR;
1084 addmodifyop( &pmods, modop, type, val.bv_val == NULL ? NULL : &val );
1088 ber_memfree( type );
1089 ber_memfree( val.bv_val );
1092 if( linenum == 0 ) {
1096 if( version && linenum == 1 ) {
1101 if ( delete_entry ) {
1102 rc = dodelete( dn );
1103 } else if ( newrdn != NULL ) {
1104 rc = dorename( dn, newrdn, newsup, deleteoldrdn );
1106 rc = domodify( dn, pmods, new_entry );
1109 if ( rc == LDAP_SUCCESS ) {
1117 if ( newrdn != NULL ) {
1120 if ( pmods != NULL ) {
1121 ldap_mods_free( pmods, 1 );
1133 struct berval *val )
1139 modop |= LDAP_MOD_BVALUES;
1142 if ( pmods != NULL ) {
1143 for ( ; pmods[ i ] != NULL; ++i ) {
1144 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
1145 pmods[ i ]->mod_op == modop )
1152 if ( pmods == NULL || pmods[ i ] == NULL ) {
1153 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
1154 sizeof( LDAPMod * ))) == NULL )
1156 perror( "realloc" );
1157 exit( EXIT_FAILURE );
1161 pmods[ i + 1 ] = NULL;
1163 pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod ));
1164 if ( pmods[ i ] == NULL ) {
1166 exit( EXIT_FAILURE );
1169 pmods[ i ]->mod_op = modop;
1170 pmods[ i ]->mod_type = ber_strdup( attr );
1171 if ( pmods[ i ]->mod_type == NULL ) {
1173 exit( EXIT_FAILURE );
1177 if ( val != NULL ) {
1179 if ( pmods[ i ]->mod_bvalues != NULL ) {
1180 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1185 pmods[ i ]->mod_bvalues = (struct berval **) ber_memrealloc(
1186 pmods[ i ]->mod_bvalues, (j + 2) * sizeof( struct berval * ));
1187 if ( pmods[ i ]->mod_bvalues == NULL ) {
1188 perror( "ber_realloc" );
1189 exit( EXIT_FAILURE );
1192 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
1193 pmods[ i ]->mod_bvalues[ j ] = ber_bvdup( val );
1194 if ( pmods[ i ]->mod_bvalues[ j ] == NULL ) {
1195 perror( "ber_bvdup" );
1196 exit( EXIT_FAILURE );
1208 int i, j, k, notascii, op;
1211 if ( pmods == NULL ) {
1212 fprintf( stderr, "%s: no attributes to change or add (entry=\"%s\")\n",
1214 return( LDAP_PARAM_ERROR );
1217 for ( i = 0; pmods[ i ] != NULL; ++i ) {
1218 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1219 if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
1221 "%s: attribute \"%s\" has no values (entry=\"%s\")\n",
1222 prog, pmods[i]->mod_type, dn );
1223 return LDAP_PARAM_ERROR;
1228 for ( i = 0; pmods[ i ] != NULL; ++i ) {
1229 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1230 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
1231 "replace" : op == LDAP_MOD_ADD ?
1232 "add" : "delete", pmods[ i ]->mod_type );
1233 if ( pmods[ i ]->mod_bvalues != NULL ) {
1234 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1235 bvp = pmods[ i ]->mod_bvalues[ j ];
1237 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
1238 if ( !isascii( bvp->bv_val[ k ] )) {
1244 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
1246 printf( "\t%s\n", bvp->bv_val );
1254 printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
1256 printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
1261 i = ldap_add_s( ld, dn, pmods );
1263 i = ldap_modify_s( ld, dn, pmods );
1265 if ( i != LDAP_SUCCESS ) {
1266 /* print error message about failed update including DN */
1267 fprintf( stderr, "%s: update failed: %s\n", prog, dn );
1268 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
1269 } else if ( verbose ) {
1270 printf( "modify complete\n" );
1288 printf( "%sdeleting entry \"%s\"\n", not ? "!" : "", dn );
1290 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
1291 fprintf( stderr, "%s: delete failed: %s\n", prog, dn );
1292 ldap_perror( ld, "ldap_delete" );
1293 } else if ( verbose ) {
1294 printf( "delete complete" );
1316 printf( "%smodifying rdn of entry \"%s\"\n", not ? "!" : "", dn );
1318 printf( "\tnew RDN: \"%s\" (%skeep existing values)\n",
1319 newrdn, deleteoldrdn ? "do not " : "" );
1322 if (( rc = ldap_rename2_s( ld, dn, newrdn, newsup, deleteoldrdn ))
1325 fprintf( stderr, "%s: rename failed: %s\n", prog, dn );
1326 ldap_perror( ld, "ldap_modrdn" );
1328 printf( "modrdn completed\n" );
1341 read_one_record( FILE *fp )
1343 char *buf, line[ LDAPMOD_MAXLINE ];
1349 while ( fgets( line, sizeof(line), fp ) != NULL ) {
1350 int len = strlen( line );
1352 if( len < 2 || ( len == 2 && *line == '\r' )) {
1360 if ( lcur + len + 1 > lmax ) {
1361 lmax = LDAPMOD_MAXLINE
1362 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
1364 if (( buf = (char *)realloc( buf, lmax )) == NULL ) {
1365 perror( "realloc" );
1366 exit( EXIT_FAILURE );
1370 strcpy( buf + lcur, line );