1 /* ldapmodify.c - generic program to modify or add entries using LDAP */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 The OpenLDAP Foundation.
6 * Portions Copyright 1998-2003 Kurt D. Zeilenga.
7 * Portions Copyright 1998-2001 Net Boolean Incorporated.
8 * Portions Copyright 2001-2003 IBM Corporation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted only as authorized by the OpenLDAP
15 * A copy of this license is available in the file LICENSE in the
16 * top-level directory of the distribution or, alternatively, at
17 * <http://www.OpenLDAP.org/license.html>.
19 /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
20 * All rights reserved.
22 * Redistribution and use in source and binary forms are permitted
23 * provided that this notice is preserved and that due credit is given
24 * to the University of Michigan at Ann Arbor. The name of the
25 * University may not be used to endorse or promote products derived
26 * from this software without specific prior written permission. This
27 * software is provided ``as is'' without express or implied warranty.
30 * This work was originally developed by the University of Michigan
31 * (as part of U-MICH LDAP). Additional significant contributors
41 #include <ac/stdlib.h>
43 #include <ac/string.h>
44 #include <ac/unistd.h>
47 #ifdef HAVE_SYS_STAT_H
51 #ifdef HAVE_SYS_FILE_H
61 #include "lutil_ldap.h"
63 #include "ldap_defaults.h"
70 static int ldapadd, force = 0;
71 static char *rejfile = NULL;
72 static LDAP *ld = NULL;
74 #define LDAPMOD_MAXLINE 4096
76 /* strings found in replog/LDIF entries (mostly lifted from slurpd/slurp.h) */
77 #define T_VERSION_STR "version"
78 #define T_REPLICA_STR "replica"
80 #define T_CONTROL_STR "control"
81 #define T_CHANGETYPESTR "changetype"
82 #define T_ADDCTSTR "add"
83 #define T_MODIFYCTSTR "modify"
84 #define T_DELETECTSTR "delete"
85 #define T_MODRDNCTSTR "modrdn"
86 #define T_MODDNCTSTR "moddn"
87 #define T_RENAMECTSTR "rename"
88 #define T_MODOPADDSTR "add"
89 #define T_MODOPREPLACESTR "replace"
90 #define T_MODOPDELETESTR "delete"
91 #define T_MODOPINCREMENTSTR "increment"
92 #define T_MODSEPSTR "-"
93 #define T_NEWRDNSTR "newrdn"
94 #define T_DELETEOLDRDNSTR "deleteoldrdn"
95 #define T_NEWSUPSTR "newsuperior"
98 static int process_ldif_rec LDAP_P(( char *rbuf, int count ));
99 static int parse_ldif_control LDAP_P(( char *line, LDAPControl ***pctrls ));
100 static void addmodifyop LDAP_P((
101 LDAPMod ***pmodsp, int modop,
103 struct berval *value ));
104 static int domodify LDAP_P((
107 LDAPControl **pctrls,
109 static int dodelete LDAP_P((
111 LDAPControl **pctrls ));
112 static int dorename LDAP_P((
117 LDAPControl **pctrls ));
118 static int process_response(
123 static char *read_one_record LDAP_P(( FILE *fp ));
125 #ifdef LDAP_GROUP_TRANSACTION
127 static int txnabort = 0;
133 fprintf( stderr, _("Add or modify entries from an LDAP server\n\n"));
134 fprintf( stderr, _("usage: %s [options]\n"), prog);
135 fprintf( stderr, _(" The list of desired operations are read from stdin"
136 " or from the file\n"));
137 fprintf( stderr, _(" specified by \"-f file\".\n"));
138 fprintf( stderr, _("Add or modify options:\n"));
139 fprintf( stderr, _(" -a add values (%s)\n"),
140 (ldapadd ? _("default") : _("default is to replace")));
141 fprintf( stderr, _(" -E [!]ext=extparam modify extensions"
142 " (! indicate s criticality)\n"));
143 #ifdef LDAP_GROUP_TRANSACTION
145 _(" [!]txn (transaction)\n"));
147 fprintf( stderr, _(" -F force all changes records to be used\n"));
148 fprintf( stderr, _(" -S file write skipped modifications to `file'\n"));
151 exit( EXIT_FAILURE );
155 const char options[] = "aE:FrS:"
156 "cd:D:e:f:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
159 handle_private_option( int i )
161 char *control, *cvalue;
165 case 'E': /* modify extensions */
166 if( protocol == LDAP_VERSION2 ) {
167 fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
169 exit( EXIT_FAILURE );
172 /* should be extended to support comma separated list of
173 * [!]key[=value] parameters, e.g. -E !foo,bar=567
178 if( optarg[0] == '!' ) {
183 control = ber_strdup( optarg );
184 if ( (cvalue = strchr( control, '=' )) != NULL ) {
188 #ifdef LDAP_GROUP_TRANSACTION
189 if( strcasecmp( control, "txn" ) == 0 ) {
193 _("txn control previously specified\n"));
194 exit( EXIT_FAILURE );
196 if( cvalue != NULL ) {
197 if( strcasecmp( cvalue, "abort" ) == 0 ) {
199 } else if( strcasecmp( cvalue, "commit" ) != 0 ) {
200 fprintf( stderr, _("Invalid value for txn control, %s\n"),
202 exit( EXIT_FAILURE );
210 fprintf( stderr, _("Invalid modify extension name: %s\n"),
219 case 'F': /* force all changes records to be used */
223 case 'r': /* replace (obsolete) */
226 case 'S': /* skipped modifications to file */
227 if( rejfile != NULL ) {
228 fprintf( stderr, _("%s: -S previously specified\n"), prog );
229 exit( EXIT_FAILURE );
231 rejfile = ber_strdup( optarg );
242 main( int argc, char **argv )
244 #ifdef LDAP_GROUP_TRANSACTION
246 struct berval txnCookie = { 0, NULL };
248 char *rbuf, *start, *rejbuf = NULL;
250 char *matched_msg, *error_msg;
255 prog = lutil_progname( "ldapmodify", argc, argv );
257 /* strncmp instead of strcmp since NT binaries carry .exe extension */
258 ldapadd = ( strncasecmp( prog, "ldapadd", sizeof("ldapadd")-1 ) == 0 );
260 tool_args( argc, argv );
262 if ( argc != optind ) usage();
264 if ( rejfile != NULL ) {
265 if (( rejfp = fopen( rejfile, "w" )) == NULL ) {
267 return( EXIT_FAILURE );
273 if ( infile != NULL ) {
274 if (( fp = fopen( infile, "r" )) == NULL ) {
276 return( EXIT_FAILURE );
282 if ( debug ) ldif_debug = debug;
284 ld = tool_conn_setup( not, 0 );
287 if ( pw_file || want_bindpw ) {
289 rc = lutil_get_filed_password( pw_file, &passwd );
290 if( rc ) return EXIT_FAILURE;
292 passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
293 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
299 #ifdef LDAP_GROUP_TRANSACTION
301 struct berval *txnCookiep = &txnCookie;
303 /* create transaction */
304 rc = ldap_txn_create_s( ld, &txnCookiep, NULL, NULL );
305 if( rc != LDAP_SUCCESS ) {
306 ldap_perror( ld, "ldap_txn_create_s" );
307 if( txn > 2 ) return EXIT_FAILURE;
313 if ( assertion || authzid || manageDIT || manageDSAit || noop
314 #ifdef LDAP_GROUP_TRANSACTION
317 || preread || postread )
323 #ifdef LDAP_GROUP_TRANSACTION
325 txnber = ber_alloc_t( LBER_USE_DER );
326 if( txnber == NULL ) return EXIT_FAILURE;
328 err = ber_printf( txnber, "{o}", &txnCookie );
330 ber_free( txnber, 1 );
331 fprintf( stderr, _("txn grouping control encoding error!\n") );
335 err = ber_flatten2( txnber, &c[i].ldctl_value, 0 );
336 if( err == -1 ) return EXIT_FAILURE;
338 c[i].ldctl_oid = LDAP_CONTROL_GROUPING;
339 c[i].ldctl_iscritical = 1;
344 tool_server_controls( ld, c, i );
350 while (( rc == 0 || contoper ) &&
351 ( rbuf = read_one_record( fp )) != NULL )
358 len = strlen( rbuf );
359 if (( rejbuf = (char *)ber_memalloc( len+1 )) == NULL ) {
361 exit( EXIT_FAILURE );
363 memcpy( rejbuf, rbuf, len+1 );
366 rc = process_ldif_rec( start, count );
368 if ( rc ) retval = rc;
370 fprintf(rejfp, _("# Error: %s (%d)"), ldap_err2string(rc), rc);
373 ldap_get_option(ld, LDAP_OPT_MATCHED_DN, &matched_msg);
374 if ( matched_msg != NULL ) {
375 if ( *matched_msg != '\0' ) {
376 fprintf( rejfp, _(", matched DN: %s"), matched_msg );
378 ldap_memfree( matched_msg );
382 ldap_get_option(ld, LDAP_OPT_ERROR_STRING, &error_msg);
383 if ( error_msg != NULL ) {
384 if ( *error_msg != '\0' ) {
385 fprintf( rejfp, _(", additional info: %s"), error_msg );
387 ldap_memfree( error_msg );
389 fprintf( rejfp, "\n%s\n", rejbuf );
392 if (rejfp) free( rejbuf );
396 #ifdef LDAP_GROUP_TRANSACTION
398 /* create transaction */
399 rc = ldap_txn_end_s( ld, &txnCookie, !txnabort, NULL, NULL );
400 if( rc != LDAP_SUCCESS ) {
401 ldap_perror( ld, "ldap_txn_create_s" );
402 if( txn > 2 ) return EXIT_FAILURE;
412 if ( rejfp != NULL ) {
422 process_ldif_rec( char *rbuf, int count )
424 char *line, *dn, *type, *newrdn, *newsup, *p;
425 int rc, linenum, modop, replicaport;
426 int expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
427 int expect_deleteoldrdn, deleteoldrdn;
428 int saw_replica, use_record, new_entry, delete_entry, got_all;
432 LDAPControl **pctrls;
436 rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
437 expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
438 expect_sep = expect_ct = 0;
445 dn = newrdn = newsup = NULL;
447 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
450 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
456 if ( ldif_parse_line( line, &type, &val.bv_val, &val.bv_len ) < 0 ) {
457 fprintf( stderr, _("%s: invalid format (line %d) entry: \"%s\"\n"),
458 prog, linenum, dn == NULL ? "" : dn );
459 rc = LDAP_PARAM_ERROR;
464 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
466 if (( p = strchr( val.bv_val, ':' )) == NULL ) {
470 replicaport = atoi( p );
472 if ( ldaphost != NULL &&
473 strcasecmp( val.bv_val, ldaphost ) == 0 &&
474 replicaport == ldapport )
478 } else if ( count == 1 && linenum == 1 &&
479 strcasecmp( type, T_VERSION_STR ) == 0 )
481 if( val.bv_len == 0 || atoi(val.bv_val) != 1 ) {
483 _("%s: invalid version %s, line %d (ignored)\n"),
484 prog, val.bv_val, linenum );
488 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
489 if (( dn = ber_strdup( val.bv_val )) == NULL ) {
491 exit( EXIT_FAILURE );
495 goto end_line; /* skip all lines until we see "dn:" */
499 /* Check for "control" tag after dn and before changetype. */
500 if (strcasecmp(type, T_CONTROL_STR) == 0) {
501 /* Parse and add it to the list of controls */
502 rc = parse_ldif_control( line, &pctrls );
505 _("%s: Error processing %s line, line %d: %s\n"),
506 prog, T_CONTROL_STR, linenum, ldap_err2string(rc) );
512 if ( !use_record && saw_replica ) {
513 printf(_("%s: skipping change record for entry: %s\n"),
515 printf(_("\t(LDAP host/port does not match replica: lines)\n"));
518 ber_memfree( val.bv_val );
522 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
523 #ifdef LIBERAL_CHANGETYPE_MODOP
524 /* trim trailing spaces (and log warning ...) */
526 for ( icnt = val.bv_len; --icnt > 0; ) {
527 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
532 if ( ++icnt != val.bv_len ) {
533 fprintf( stderr, _("%s: illegal trailing space after"
534 " \"%s: %s\" trimmed (line %d of entry \"%s\")\n"),
535 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
536 val.bv_val[icnt] = '\0';
538 #endif /* LIBERAL_CHANGETYPE_MODOP */
540 if ( strcasecmp( val.bv_val, T_MODIFYCTSTR ) == 0 ) {
543 } else if ( strcasecmp( val.bv_val, T_ADDCTSTR ) == 0 ) {
545 } else if ( strcasecmp( val.bv_val, T_MODRDNCTSTR ) == 0
546 || strcasecmp( val.bv_val, T_MODDNCTSTR ) == 0
547 || strcasecmp( val.bv_val, T_RENAMECTSTR ) == 0)
550 } else if ( strcasecmp( val.bv_val, T_DELETECTSTR ) == 0 ) {
551 got_all = delete_entry = 1;
554 _("%s: unknown %s \"%s\" (line %d of entry \"%s\")\n"),
555 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
556 rc = LDAP_PARAM_ERROR;
559 } else if ( ldapadd ) { /* missing changetype => add */
561 modop = LDAP_MOD_ADD;
563 expect_modop = 1; /* missing changetype => modify */
567 if ( expect_modop ) {
568 #ifdef LIBERAL_CHANGETYPE_MODOP
569 /* trim trailing spaces (and log warning ...) */
571 for ( icnt = val.bv_len; --icnt > 0; ) {
572 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) break;
575 if ( ++icnt != val.bv_len ) {
576 fprintf( stderr, _("%s: illegal trailing space after"
577 " \"%s: %s\" trimmed (line %d of entry \"%s\")\n"),
578 prog, type, val.bv_val, linenum, dn );
579 val.bv_val[icnt] = '\0';
581 #endif /* LIBERAL_CHANGETYPE_MODOP */
585 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
586 modop = LDAP_MOD_ADD;
588 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
589 modop = LDAP_MOD_REPLACE;
590 addmodifyop( &pmods, modop, val.bv_val, NULL );
592 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
593 modop = LDAP_MOD_DELETE;
594 addmodifyop( &pmods, modop, val.bv_val, NULL );
596 } else if ( strcasecmp( type, T_MODOPINCREMENTSTR ) == 0 ) {
597 modop = LDAP_MOD_INCREMENT;
598 addmodifyop( &pmods, modop, val.bv_val, NULL );
600 } else { /* no modify op: use default */
601 modop = ldapadd ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
605 if ( expect_newrdn ) {
606 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
607 if (( newrdn = ber_strdup( val.bv_val )) == NULL ) {
609 exit( EXIT_FAILURE );
611 expect_deleteoldrdn = 1;
614 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
615 " \"%s:\" (line %d of entry \"%s\")\n"),
616 prog, T_NEWRDNSTR, type, linenum, dn );
617 rc = LDAP_PARAM_ERROR;
619 } else if ( expect_deleteoldrdn ) {
620 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
621 deleteoldrdn = ( *val.bv_val == '0' ) ? 0 : 1;
622 expect_deleteoldrdn = 0;
626 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
627 " \"%s:\" (line %d of entry \"%s\")\n"),
628 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
629 rc = LDAP_PARAM_ERROR;
631 } else if ( expect_newsup ) {
632 if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
633 if (( newsup = ber_strdup( val.bv_val )) == NULL ) {
635 exit( EXIT_FAILURE );
639 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
640 " \"%s:\" (line %d of entry \"%s\")\n"),
641 prog, T_NEWSUPSTR, type, linenum, dn );
642 rc = LDAP_PARAM_ERROR;
644 } else if ( got_all ) {
646 _("%s: extra lines at end (line %d of entry \"%s\")\n"),
648 rc = LDAP_PARAM_ERROR;
650 addmodifyop( &pmods, modop, type, &val );
655 ber_memfree( val.bv_val );
662 if( version && linenum == 1 ) {
666 /* If default controls are set (as with -M option) and controls are
667 specified in the LDIF file, we must add the default controls to
668 the list of controls sent with the ldap operation.
672 LDAPControl **defctrls = NULL; /* Default server controls */
673 LDAPControl **newctrls = NULL;
674 ldap_get_option(ld, LDAP_OPT_SERVER_CONTROLS, &defctrls);
676 int npc=0; /* Num of LDIF controls */
677 int ndefc=0; /* Num of default controls */
678 while (pctrls[npc]) npc++; /* Count LDIF controls */
679 while (defctrls[ndefc]) ndefc++; /* Count default controls */
680 newctrls = ber_memrealloc(pctrls,
681 (npc+ndefc+1)*sizeof(LDAPControl*));
683 if (newctrls == NULL) {
688 for (i=npc; i<npc+ndefc; i++) {
689 pctrls[i] = ldap_control_dup(defctrls[i-npc]);
690 if (pctrls[i] == NULL) {
695 pctrls[npc+ndefc] = NULL;
697 ldap_controls_free(defctrls); /* Must be freed by library */
704 if ( delete_entry ) {
705 rc = dodelete( dn, pctrls );
706 } else if ( newrdn != NULL ) {
707 rc = dorename( dn, newrdn, newsup, deleteoldrdn, pctrls );
709 rc = domodify( dn, pmods, pctrls, new_entry );
712 if ( rc == LDAP_SUCCESS ) {
720 if ( newrdn != NULL ) {
723 if ( pmods != NULL ) {
724 ldap_mods_free( pmods, 1 );
726 if (pctrls != NULL) {
727 ldap_controls_free( pctrls );
733 /* Parse an LDIF control line of the form
734 control: oid [true/false] [: value] or
735 control: oid [true/false] [:: base64-value] or
736 control: oid [true/false] [:< url]
737 The control is added to the list of controls in *ppctrls.
742 LDAPControl ***ppctrls )
745 int criticality = 0; /* Default is false if not present */
748 ber_len_t value_len = 0;
750 char *s, *oidStart, *pcolon;
751 LDAPControl *newctrl = NULL;
752 LDAPControl **pctrls = NULL;
754 if (ppctrls) pctrls = *ppctrls;
755 s = line + strlen(T_CONTROL_STR); /* Skip over "control" */
756 pcolon = s; /* Save this position for later */
757 if (*s++ != ':') { /* Make sure colon follows */
758 return ( LDAP_PARAM_ERROR );
760 while (*s && isspace((unsigned char)*s)) {
761 s++; /* Skip white space before OID */
764 /* OID should come next. Validate and extract it. */
765 if (*s == 0) return ( LDAP_PARAM_ERROR );
767 while (isdigit((unsigned char)*s) || *s == '.') {
768 s++; /* OID should be digits or . */
771 return ( LDAP_PARAM_ERROR ); /* OID was not present */
773 if (*s) { /* End of OID should be space or NULL */
774 if (!isspace((unsigned char)*s)) {
775 return ( LDAP_PARAM_ERROR ); /* else OID contained invalid chars */
777 *s++ = 0; /* Replace space with null to terminate */
780 oid = ber_strdup(oidStart);
781 if (oid == NULL) return ( LDAP_NO_MEMORY );
783 /* Optional Criticality field is next. */
784 while (*s && isspace((unsigned char)*s)) {
785 s++; /* Skip white space before criticality */
787 if (strncasecmp(s, "true", 4) == 0) {
791 else if (strncasecmp(s, "false", 5) == 0) {
796 /* Optional value field is next */
797 while (*s && isspace((unsigned char)*s)) {
798 s++; /* Skip white space before value */
801 if (*s != ':') { /* If value is present, must start with : */
802 rc = LDAP_PARAM_ERROR;
806 /* Shift value down over OID and criticality so it's in the form
808 control:: base64-value
810 Then we can use ldif_parse_line to extract and decode the value
812 while ( (*pcolon++ = *s++) != 0) { /* Shift value */
815 rc = ldif_parse_line(line, &type, &val, &value_len);
816 if (type) ber_memfree(type); /* Don't need this field*/
818 rc = LDAP_PARAM_ERROR;
823 /* Create a new LDAPControl structure. */
824 newctrl = (LDAPControl *)ber_memalloc(sizeof(LDAPControl));
825 if ( newctrl == NULL ) {
829 newctrl->ldctl_oid = oid;
831 newctrl->ldctl_iscritical = criticality;
832 newctrl->ldctl_value.bv_len = value_len;
833 newctrl->ldctl_value.bv_val = val;
836 /* Add the new control to the passed-in list of controls. */
839 while ( pctrls[i] ) { /* Count the # of controls passed in */
843 /* Allocate 1 more slot for the new control and 1 for the NULL. */
844 pctrls = (LDAPControl **) ber_memrealloc(pctrls,
845 (i+2)*(sizeof(LDAPControl *)));
846 if (pctrls == NULL) {
857 if (newctrl->ldctl_oid) ber_memfree(newctrl->ldctl_oid);
858 if (newctrl->ldctl_value.bv_val) {
859 ber_memfree(newctrl->ldctl_value.bv_val);
861 ber_memfree(newctrl);
863 if (val) ber_memfree(val);
864 if (oid) ber_memfree(oid);
881 modop |= LDAP_MOD_BVALUES;
884 if ( pmods != NULL ) {
885 for ( ; pmods[ i ] != NULL; ++i ) {
886 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
887 pmods[ i ]->mod_op == modop )
894 if ( pmods == NULL || pmods[ i ] == NULL ) {
895 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
896 sizeof( LDAPMod * ))) == NULL )
899 exit( EXIT_FAILURE );
903 pmods[ i + 1 ] = NULL;
905 pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod ));
906 if ( pmods[ i ] == NULL ) {
908 exit( EXIT_FAILURE );
911 pmods[ i ]->mod_op = modop;
912 pmods[ i ]->mod_type = ber_strdup( attr );
913 if ( pmods[ i ]->mod_type == NULL ) {
915 exit( EXIT_FAILURE );
921 if ( pmods[ i ]->mod_bvalues != NULL ) {
922 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
927 pmods[ i ]->mod_bvalues = (struct berval **) ber_memrealloc(
928 pmods[ i ]->mod_bvalues, (j + 2) * sizeof( struct berval * ));
929 if ( pmods[ i ]->mod_bvalues == NULL ) {
930 perror( "ber_realloc" );
931 exit( EXIT_FAILURE );
934 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
935 pmods[ i ]->mod_bvalues[ j ] = ber_bvdup( val );
936 if ( pmods[ i ]->mod_bvalues[ j ] == NULL ) {
937 perror( "ber_bvdup" );
938 exit( EXIT_FAILURE );
948 LDAPControl **pctrls,
951 int rc, i, j, k, notascii, op;
955 fprintf( stderr, _("%s: no DN specified\n"), prog );
956 return( LDAP_PARAM_ERROR );
959 if ( pmods == NULL ) {
961 _("%s: no attributes to change or add (entry=\"%s\")\n"),
963 return( LDAP_PARAM_ERROR );
966 for ( i = 0; pmods[ i ] != NULL; ++i ) {
967 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
968 if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
970 _("%s: attribute \"%s\" has no values (entry=\"%s\")\n"),
971 prog, pmods[i]->mod_type, dn );
972 return LDAP_PARAM_ERROR;
977 for ( i = 0; pmods[ i ] != NULL; ++i ) {
978 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
980 op == LDAP_MOD_REPLACE ? _("replace") :
981 op == LDAP_MOD_ADD ? _("add") :
982 op == LDAP_MOD_INCREMENT ? _("increment") :
983 op == LDAP_MOD_DELETE ? _("delete") :
985 pmods[ i ]->mod_type );
987 if ( pmods[ i ]->mod_bvalues != NULL ) {
988 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
989 bvp = pmods[ i ]->mod_bvalues[ j ];
991 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
992 if ( !isascii( bvp->bv_val[ k ] )) {
998 printf( _("\tNOT ASCII (%ld bytes)\n"), bvp->bv_len );
1000 printf( "\t%s\n", bvp->bv_val );
1008 printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
1010 printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
1016 rc = ldap_add_ext( ld, dn, pmods, pctrls, NULL, &msgid );
1018 rc = ldap_modify_ext( ld, dn, pmods, pctrls, NULL, &msgid );
1021 if ( rc != LDAP_SUCCESS ) {
1022 /* print error message about failed update including DN */
1023 fprintf( stderr, _("%s: update failed: %s\n"), prog, dn );
1024 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
1026 } else if ( verbose ) {
1027 printf( _("modify complete\n") );
1030 rc = process_response( ld, msgid,
1031 newentry ? "ldap_add" : "ldap_modify", dn );
1046 LDAPControl **pctrls )
1051 printf( _("%sdeleting entry \"%s\"\n"), not ? "!" : "", dn );
1053 rc = ldap_delete_ext( ld, dn, pctrls, NULL, &msgid );
1054 if ( rc != LDAP_SUCCESS ) {
1055 fprintf( stderr, _("%s: delete failed: %s\n"), prog, dn );
1056 ldap_perror( ld, "ldap_delete" );
1058 } else if ( verbose ) {
1059 printf( _("delete complete") );
1062 rc = process_response( ld, msgid, "ldap_delete", dn );
1080 LDAPControl **pctrls )
1085 printf( _("%smodifying rdn of entry \"%s\"\n"), not ? "!" : "", dn );
1087 printf( _("\tnew RDN: \"%s\" (%skeep existing values)\n"),
1088 newrdn, deleteoldrdn ? _("do not ") : "" );
1091 rc = ldap_rename( ld, dn, newrdn, newsup, deleteoldrdn,
1092 pctrls, NULL, &msgid );
1093 if ( rc != LDAP_SUCCESS ) {
1094 fprintf( stderr, _("%s: rename failed: %s\n"), prog, dn );
1095 ldap_perror( ld, "ldap_modrdn" );
1098 printf( _("modrdn completed\n") );
1101 rc = process_response( ld, msgid, "ldap_rename", dn );
1112 static int process_response(
1119 int rc = LDAP_OTHER;
1120 struct timeval tv = { 0 };
1124 tv.tv_usec = 100000;
1126 rc = ldap_result( ld, msgid,
1127 #ifdef LDAP_GROUP_TRANSACTION
1133 if ( tool_check_abandon( ld, msgid ) ) {
1134 return LDAP_CANCELLED;
1138 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
1148 if ( ldap_msgtype( res ) != LDAP_RES_INTERMEDIATE ) {
1149 rc = ldap_result2error( ld, res, 1 );
1150 if( rc != LDAP_SUCCESS ) ldap_perror( ld, opstr );
1154 #ifdef LDAP_GROUP_TRANSACTION
1155 /* assume (successful) transaction intermediate response */
1156 return LDAP_SUCCESS;
1159 /* intermediate response? */
1160 return LDAP_DECODING_ERROR;
1165 read_one_record( FILE *fp )
1167 char *buf, line[ LDAPMOD_MAXLINE ];
1173 while ( fgets( line, sizeof(line), fp ) != NULL ) {
1174 int len = strlen( line );
1176 if( len < 2 || ( len == 2 && *line == '\r' )) {
1184 if ( lcur + len + 1 > lmax ) {
1185 lmax = LDAPMOD_MAXLINE
1186 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
1188 if (( buf = (char *)ber_memrealloc( buf, lmax )) == NULL ) {
1189 perror( "realloc" );
1190 exit( EXIT_FAILURE );
1194 strcpy( buf + lcur, line );