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-2004 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>
46 #ifdef HAVE_SYS_STAT_H
50 #ifdef HAVE_SYS_FILE_H
60 #include "lutil_ldap.h"
62 #include "ldap_defaults.h"
69 static int ldapadd, force = 0;
70 static char *rejfile = NULL;
71 static LDAP *ld = NULL;
73 #define LDAPMOD_MAXLINE 4096
75 /* strings found in replog/LDIF entries (mostly lifted from slurpd/slurp.h) */
76 #define T_VERSION_STR "version"
77 #define T_REPLICA_STR "replica"
79 #define T_CONTROL_STR "control"
80 #define T_CHANGETYPESTR "changetype"
81 #define T_ADDCTSTR "add"
82 #define T_MODIFYCTSTR "modify"
83 #define T_DELETECTSTR "delete"
84 #define T_MODRDNCTSTR "modrdn"
85 #define T_MODDNCTSTR "moddn"
86 #define T_RENAMECTSTR "rename"
87 #define T_MODOPADDSTR "add"
88 #define T_MODOPREPLACESTR "replace"
89 #define T_MODOPDELETESTR "delete"
90 #define T_MODOPINCREMENTSTR "increment"
91 #define T_MODSEPSTR "-"
92 #define T_NEWRDNSTR "newrdn"
93 #define T_DELETEOLDRDNSTR "deleteoldrdn"
94 #define T_NEWSUPSTR "newsuperior"
97 static int process_ldif_rec LDAP_P(( char *rbuf, int count ));
98 static int parse_ldif_control LDAP_P(( char *line, LDAPControl ***pctrls ));
99 static void addmodifyop LDAP_P((
100 LDAPMod ***pmodsp, int modop,
102 struct berval *value ));
103 static int domodify LDAP_P((
106 LDAPControl **pctrls,
108 static int dodelete LDAP_P((
110 LDAPControl **pctrls ));
111 static int dorename LDAP_P((
116 LDAPControl **pctrls ));
117 static int process_response(
122 static char *read_one_record LDAP_P(( FILE *fp ));
124 #ifdef LDAP_GROUP_TRANSACTION
126 static int txnabort = 0;
132 fprintf( stderr, _("Add or modify entries from an LDAP server\n\n"));
133 fprintf( stderr, _("usage: %s [options]\n"), prog);
134 fprintf( stderr, _(" The list of desired operations are read from stdin"
135 " or from the file\n"));
136 fprintf( stderr, _(" specified by \"-f file\".\n"));
137 fprintf( stderr, _("Add or modify options:\n"));
138 fprintf( stderr, _(" -a add values (%s)\n"),
139 (ldapadd ? _("default") : _("default is to replace")));
140 fprintf( stderr, _(" -E [!]ext=extparam modify extensions"
141 " (! indicate s criticality)\n"));
142 #ifdef LDAP_GROUP_TRANSACTION
144 _(" [!]txn (transaction)\n"));
146 fprintf( stderr, _(" -F force all changes records to be used\n"));
147 fprintf( stderr, _(" -S file write skipped modifications to `file'\n"));
150 exit( EXIT_FAILURE );
154 const char options[] = "aE:FrS:"
155 "cd:D:e:f:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
158 handle_private_option( int i )
160 char *control, *cvalue;
164 case 'E': /* modify extensions */
165 if( protocol == LDAP_VERSION2 ) {
166 fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
168 exit( EXIT_FAILURE );
171 /* should be extended to support comma separated list of
172 * [!]key[=value] parameters, e.g. -E !foo,bar=567
177 if( optarg[0] == '!' ) {
182 control = ber_strdup( optarg );
183 if ( (cvalue = strchr( control, '=' )) != NULL ) {
187 #ifdef LDAP_GROUP_TRANSACTION
188 if( strcasecmp( control, "txn" ) == 0 ) {
192 _("txn control previously specified\n"));
193 exit( EXIT_FAILURE );
195 if( cvalue != NULL ) {
196 if( strcasecmp( cvalue, "abort" ) == 0 ) {
198 } else if( strcasecmp( cvalue, "commit" ) != 0 ) {
199 fprintf( stderr, _("Invalid value for txn control, %s\n"),
201 exit( EXIT_FAILURE );
209 fprintf( stderr, _("Invalid modify extension name: %s\n"),
218 case 'F': /* force all changes records to be used */
222 case 'r': /* replace (obsolete) */
225 case 'S': /* skipped modifications to file */
226 if( rejfile != NULL ) {
227 fprintf( stderr, _("%s: -S previously specified\n"), prog );
228 exit( EXIT_FAILURE );
230 rejfile = ber_strdup( optarg );
241 main( int argc, char **argv )
243 #ifdef LDAP_GROUP_TRANSACTION
245 struct berval txnCookie = { 0, NULL };
247 char *rbuf, *start, *rejbuf = NULL;
249 char *matched_msg, *error_msg;
254 prog = lutil_progname( "ldapmodify", argc, argv );
256 /* strncmp instead of strcmp since NT binaries carry .exe extension */
257 ldapadd = ( strncasecmp( prog, "ldapadd", sizeof("ldapadd")-1 ) == 0 );
259 tool_args( argc, argv );
261 if ( argc != optind ) usage();
263 if ( rejfile != NULL ) {
264 if (( rejfp = fopen( rejfile, "w" )) == NULL ) {
266 return( EXIT_FAILURE );
272 if ( infile != NULL ) {
273 if (( fp = fopen( infile, "r" )) == NULL ) {
275 return( EXIT_FAILURE );
281 if ( debug ) ldif_debug = debug;
283 ld = tool_conn_setup( not, 0 );
286 if ( pw_file || want_bindpw ) {
288 rc = lutil_get_filed_password( pw_file, &passwd );
289 if( rc ) return EXIT_FAILURE;
291 passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
292 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
298 #ifdef LDAP_GROUP_TRANSACTION
300 struct berval *txnCookiep = &txnCookie;
302 /* create transaction */
303 rc = ldap_txn_create_s( ld, &txnCookiep, NULL, NULL );
304 if( rc != LDAP_SUCCESS ) {
305 ldap_perror( ld, "ldap_txn_create_s" );
306 if( txn > 2 ) return EXIT_FAILURE;
312 if ( assertion || authzid || manageDSAit || noop || preread || postread
313 #ifdef LDAP_GROUP_TRANSACTION
322 #ifdef LDAP_GROUP_TRANSACTION
324 txnber = ber_alloc_t( LBER_USE_DER );
325 if( txnber == NULL ) return EXIT_FAILURE;
327 err = ber_printf( txnber, "{o}", &txnCookie );
329 ber_free( txnber, 1 );
330 fprintf( stderr, _("txn grouping control encoding error!\n") );
334 err = ber_flatten2( txnber, &c[i].ldctl_value, 0 );
335 if( err == -1 ) return EXIT_FAILURE;
337 c[i].ldctl_oid = LDAP_CONTROL_GROUPING;
338 c[i].ldctl_iscritical = 1;
343 tool_server_controls( ld, c, i );
349 while (( rc == 0 || contoper ) &&
350 ( rbuf = read_one_record( fp )) != NULL )
357 len = strlen( rbuf );
358 if (( rejbuf = (char *)ber_memalloc( len+1 )) == NULL ) {
360 exit( EXIT_FAILURE );
362 memcpy( rejbuf, rbuf, len+1 );
365 rc = process_ldif_rec( start, count );
367 if ( rc ) retval = rc;
369 fprintf(rejfp, _("# Error: %s (%d)"), ldap_err2string(rc), rc);
372 ldap_get_option(ld, LDAP_OPT_MATCHED_DN, &matched_msg);
373 if ( matched_msg != NULL ) {
374 if ( *matched_msg != '\0' ) {
375 fprintf( rejfp, _(", matched DN: %s"), matched_msg );
377 ldap_memfree( matched_msg );
381 ldap_get_option(ld, LDAP_OPT_ERROR_STRING, &error_msg);
382 if ( error_msg != NULL ) {
383 if ( *error_msg != '\0' ) {
384 fprintf( rejfp, _(", additional info: %s"), error_msg );
386 ldap_memfree( error_msg );
388 fprintf( rejfp, "\n%s\n", rejbuf );
391 if (rejfp) free( rejbuf );
395 #ifdef LDAP_GROUP_TRANSACTION
397 /* create transaction */
398 rc = ldap_txn_end_s( ld, &txnCookie, !txnabort, NULL, NULL );
399 if( rc != LDAP_SUCCESS ) {
400 ldap_perror( ld, "ldap_txn_create_s" );
401 if( txn > 2 ) return EXIT_FAILURE;
408 ldap_unbind_ext( ld, NULL, NULL );
411 if ( rejfp != NULL ) {
420 process_ldif_rec( char *rbuf, int count )
422 char *line, *dn, *type, *newrdn, *newsup, *p;
423 int rc, linenum, modop, replicaport;
424 int expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
425 int expect_deleteoldrdn, deleteoldrdn;
426 int saw_replica, use_record, new_entry, delete_entry, got_all;
430 LDAPControl **pctrls;
434 rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
435 expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
436 expect_sep = expect_ct = 0;
443 dn = newrdn = newsup = NULL;
445 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
448 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
454 if ( ldif_parse_line( line, &type, &val.bv_val, &val.bv_len ) < 0 ) {
455 fprintf( stderr, _("%s: invalid format (line %d) entry: \"%s\"\n"),
456 prog, linenum, dn == NULL ? "" : dn );
457 rc = LDAP_PARAM_ERROR;
462 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
464 if (( p = strchr( val.bv_val, ':' )) == NULL ) {
468 replicaport = atoi( p );
470 if ( ldaphost != NULL &&
471 strcasecmp( val.bv_val, ldaphost ) == 0 &&
472 replicaport == ldapport )
476 } else if ( count == 1 && linenum == 1 &&
477 strcasecmp( type, T_VERSION_STR ) == 0 )
479 if( val.bv_len == 0 || atoi(val.bv_val) != 1 ) {
481 _("%s: invalid version %s, line %d (ignored)\n"),
482 prog, val.bv_val, linenum );
486 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
487 if (( dn = ber_strdup( val.bv_val )) == NULL ) {
489 exit( EXIT_FAILURE );
493 goto end_line; /* skip all lines until we see "dn:" */
497 /* Check for "control" tag after dn and before changetype. */
498 if (strcasecmp(type, T_CONTROL_STR) == 0) {
499 /* Parse and add it to the list of controls */
500 rc = parse_ldif_control( line, &pctrls );
503 _("%s: Error processing %s line, line %d: %s\n"),
504 prog, T_CONTROL_STR, linenum, ldap_err2string(rc) );
510 if ( !use_record && saw_replica ) {
511 printf(_("%s: skipping change record for entry: %s\n"),
513 printf(_("\t(LDAP host/port does not match replica: lines)\n"));
516 ber_memfree( val.bv_val );
520 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
521 #ifdef LIBERAL_CHANGETYPE_MODOP
522 /* trim trailing spaces (and log warning ...) */
524 for ( icnt = val.bv_len; --icnt > 0; ) {
525 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
530 if ( ++icnt != val.bv_len ) {
531 fprintf( stderr, _("%s: illegal trailing space after"
532 " \"%s: %s\" trimmed (line %d of entry \"%s\")\n"),
533 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
534 val.bv_val[icnt] = '\0';
536 #endif /* LIBERAL_CHANGETYPE_MODOP */
538 if ( strcasecmp( val.bv_val, T_MODIFYCTSTR ) == 0 ) {
541 } else if ( strcasecmp( val.bv_val, T_ADDCTSTR ) == 0 ) {
543 } else if ( strcasecmp( val.bv_val, T_MODRDNCTSTR ) == 0
544 || strcasecmp( val.bv_val, T_MODDNCTSTR ) == 0
545 || strcasecmp( val.bv_val, T_RENAMECTSTR ) == 0)
548 } else if ( strcasecmp( val.bv_val, T_DELETECTSTR ) == 0 ) {
549 got_all = delete_entry = 1;
552 _("%s: unknown %s \"%s\" (line %d of entry \"%s\")\n"),
553 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
554 rc = LDAP_PARAM_ERROR;
557 } else if ( ldapadd ) { /* missing changetype => add */
559 modop = LDAP_MOD_ADD;
561 expect_modop = 1; /* missing changetype => modify */
565 if ( expect_modop ) {
566 #ifdef LIBERAL_CHANGETYPE_MODOP
567 /* trim trailing spaces (and log warning ...) */
569 for ( icnt = val.bv_len; --icnt > 0; ) {
570 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) break;
573 if ( ++icnt != val.bv_len ) {
574 fprintf( stderr, _("%s: illegal trailing space after"
575 " \"%s: %s\" trimmed (line %d of entry \"%s\")\n"),
576 prog, type, val.bv_val, linenum, dn );
577 val.bv_val[icnt] = '\0';
579 #endif /* LIBERAL_CHANGETYPE_MODOP */
583 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
584 modop = LDAP_MOD_ADD;
586 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
587 modop = LDAP_MOD_REPLACE;
588 addmodifyop( &pmods, modop, val.bv_val, NULL );
590 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
591 modop = LDAP_MOD_DELETE;
592 addmodifyop( &pmods, modop, val.bv_val, NULL );
594 } else if ( strcasecmp( type, T_MODOPINCREMENTSTR ) == 0 ) {
595 modop = LDAP_MOD_INCREMENT;
596 addmodifyop( &pmods, modop, val.bv_val, NULL );
598 } else { /* no modify op: use default */
599 modop = ldapadd ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
603 if ( expect_newrdn ) {
604 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
605 if (( newrdn = ber_strdup( val.bv_val )) == NULL ) {
607 exit( EXIT_FAILURE );
609 expect_deleteoldrdn = 1;
612 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
613 " \"%s:\" (line %d of entry \"%s\")\n"),
614 prog, T_NEWRDNSTR, type, linenum, dn );
615 rc = LDAP_PARAM_ERROR;
617 } else if ( expect_deleteoldrdn ) {
618 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
619 deleteoldrdn = ( *val.bv_val == '0' ) ? 0 : 1;
620 expect_deleteoldrdn = 0;
624 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
625 " \"%s:\" (line %d of entry \"%s\")\n"),
626 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
627 rc = LDAP_PARAM_ERROR;
629 } else if ( expect_newsup ) {
630 if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
631 if (( newsup = ber_strdup( val.bv_val )) == NULL ) {
633 exit( EXIT_FAILURE );
637 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
638 " \"%s:\" (line %d of entry \"%s\")\n"),
639 prog, T_NEWSUPSTR, type, linenum, dn );
640 rc = LDAP_PARAM_ERROR;
642 } else if ( got_all ) {
644 _("%s: extra lines at end (line %d of entry \"%s\")\n"),
646 rc = LDAP_PARAM_ERROR;
648 addmodifyop( &pmods, modop, type, &val );
653 ber_memfree( val.bv_val );
660 if( version && linenum == 1 ) {
664 /* If default controls are set (as with -M option) and controls are
665 specified in the LDIF file, we must add the default controls to
666 the list of controls sent with the ldap operation.
670 LDAPControl **defctrls = NULL; /* Default server controls */
671 LDAPControl **newctrls = NULL;
672 ldap_get_option(ld, LDAP_OPT_SERVER_CONTROLS, &defctrls);
674 int npc=0; /* Num of LDIF controls */
675 int ndefc=0; /* Num of default controls */
676 while (pctrls[npc]) npc++; /* Count LDIF controls */
677 while (defctrls[ndefc]) ndefc++; /* Count default controls */
678 newctrls = ber_memrealloc(pctrls,
679 (npc+ndefc+1)*sizeof(LDAPControl*));
681 if (newctrls == NULL) {
686 for (i=npc; i<npc+ndefc; i++) {
687 pctrls[i] = ldap_control_dup(defctrls[i-npc]);
688 if (pctrls[i] == NULL) {
693 pctrls[npc+ndefc] = NULL;
695 ldap_controls_free(defctrls); /* Must be freed by library */
702 if ( delete_entry ) {
703 rc = dodelete( dn, pctrls );
704 } else if ( newrdn != NULL ) {
705 rc = dorename( dn, newrdn, newsup, deleteoldrdn, pctrls );
707 rc = domodify( dn, pmods, pctrls, new_entry );
710 if ( rc == LDAP_SUCCESS ) {
718 if ( newrdn != NULL ) {
721 if ( pmods != NULL ) {
722 ldap_mods_free( pmods, 1 );
724 if (pctrls != NULL) {
725 ldap_controls_free( pctrls );
731 /* Parse an LDIF control line of the form
732 control: oid [true/false] [: value] or
733 control: oid [true/false] [:: base64-value] or
734 control: oid [true/false] [:< url]
735 The control is added to the list of controls in *ppctrls.
740 LDAPControl ***ppctrls )
743 int criticality = 0; /* Default is false if not present */
746 ber_len_t value_len = 0;
748 char *s, *oidStart, *pcolon;
749 LDAPControl *newctrl = NULL;
750 LDAPControl **pctrls = NULL;
752 if (ppctrls) pctrls = *ppctrls;
753 s = line + strlen(T_CONTROL_STR); /* Skip over "control" */
754 pcolon = s; /* Save this position for later */
755 if (*s++ != ':') { /* Make sure colon follows */
756 return ( LDAP_PARAM_ERROR );
758 while (*s && isspace((unsigned char)*s)) {
759 s++; /* Skip white space before OID */
762 /* OID should come next. Validate and extract it. */
763 if (*s == 0) return ( LDAP_PARAM_ERROR );
765 while (isdigit((unsigned char)*s) || *s == '.') {
766 s++; /* OID should be digits or . */
769 return ( LDAP_PARAM_ERROR ); /* OID was not present */
771 if (*s) { /* End of OID should be space or NULL */
772 if (!isspace((unsigned char)*s)) {
773 return ( LDAP_PARAM_ERROR ); /* else OID contained invalid chars */
775 *s++ = 0; /* Replace space with null to terminate */
778 oid = ber_strdup(oidStart);
779 if (oid == NULL) return ( LDAP_NO_MEMORY );
781 /* Optional Criticality field is next. */
782 while (*s && isspace((unsigned char)*s)) {
783 s++; /* Skip white space before criticality */
785 if (strncasecmp(s, "true", 4) == 0) {
789 else if (strncasecmp(s, "false", 5) == 0) {
794 /* Optional value field is next */
795 while (*s && isspace((unsigned char)*s)) {
796 s++; /* Skip white space before value */
799 if (*s != ':') { /* If value is present, must start with : */
800 rc = LDAP_PARAM_ERROR;
804 /* Shift value down over OID and criticality so it's in the form
806 control:: base64-value
808 Then we can use ldif_parse_line to extract and decode the value
810 while ( (*pcolon++ = *s++) != 0) { /* Shift value */
813 rc = ldif_parse_line(line, &type, &val, &value_len);
814 if (type) ber_memfree(type); /* Don't need this field*/
816 rc = LDAP_PARAM_ERROR;
821 /* Create a new LDAPControl structure. */
822 newctrl = (LDAPControl *)ber_memalloc(sizeof(LDAPControl));
823 if ( newctrl == NULL ) {
827 newctrl->ldctl_oid = oid;
829 newctrl->ldctl_iscritical = criticality;
830 newctrl->ldctl_value.bv_len = value_len;
831 newctrl->ldctl_value.bv_val = val;
834 /* Add the new control to the passed-in list of controls. */
837 while ( pctrls[i] ) { /* Count the # of controls passed in */
841 /* Allocate 1 more slot for the new control and 1 for the NULL. */
842 pctrls = (LDAPControl **) ber_memrealloc(pctrls,
843 (i+2)*(sizeof(LDAPControl *)));
844 if (pctrls == NULL) {
855 if (newctrl->ldctl_oid) ber_memfree(newctrl->ldctl_oid);
856 if (newctrl->ldctl_value.bv_val) {
857 ber_memfree(newctrl->ldctl_value.bv_val);
859 ber_memfree(newctrl);
861 if (val) ber_memfree(val);
862 if (oid) ber_memfree(oid);
879 modop |= LDAP_MOD_BVALUES;
882 if ( pmods != NULL ) {
883 for ( ; pmods[ i ] != NULL; ++i ) {
884 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
885 pmods[ i ]->mod_op == modop )
892 if ( pmods == NULL || pmods[ i ] == NULL ) {
893 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
894 sizeof( LDAPMod * ))) == NULL )
897 exit( EXIT_FAILURE );
901 pmods[ i + 1 ] = NULL;
903 pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod ));
904 if ( pmods[ i ] == NULL ) {
906 exit( EXIT_FAILURE );
909 pmods[ i ]->mod_op = modop;
910 pmods[ i ]->mod_type = ber_strdup( attr );
911 if ( pmods[ i ]->mod_type == NULL ) {
913 exit( EXIT_FAILURE );
919 if ( pmods[ i ]->mod_bvalues != NULL ) {
920 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
925 pmods[ i ]->mod_bvalues = (struct berval **) ber_memrealloc(
926 pmods[ i ]->mod_bvalues, (j + 2) * sizeof( struct berval * ));
927 if ( pmods[ i ]->mod_bvalues == NULL ) {
928 perror( "ber_realloc" );
929 exit( EXIT_FAILURE );
932 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
933 pmods[ i ]->mod_bvalues[ j ] = ber_bvdup( val );
934 if ( pmods[ i ]->mod_bvalues[ j ] == NULL ) {
935 perror( "ber_bvdup" );
936 exit( EXIT_FAILURE );
946 LDAPControl **pctrls,
949 int rc, i, j, k, notascii, op;
953 fprintf( stderr, _("%s: no DN specified\n"), prog );
954 return( LDAP_PARAM_ERROR );
957 if ( pmods == NULL ) {
959 _("%s: no attributes to change or add (entry=\"%s\")\n"),
961 return( LDAP_PARAM_ERROR );
964 for ( i = 0; pmods[ i ] != NULL; ++i ) {
965 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
966 if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
968 _("%s: attribute \"%s\" has no values (entry=\"%s\")\n"),
969 prog, pmods[i]->mod_type, dn );
970 return LDAP_PARAM_ERROR;
975 for ( i = 0; pmods[ i ] != NULL; ++i ) {
976 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
978 op == LDAP_MOD_REPLACE ? _("replace") :
979 op == LDAP_MOD_ADD ? _("add") :
980 op == LDAP_MOD_INCREMENT ? _("increment") :
981 op == LDAP_MOD_DELETE ? _("delete") :
983 pmods[ i ]->mod_type );
985 if ( pmods[ i ]->mod_bvalues != NULL ) {
986 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
987 bvp = pmods[ i ]->mod_bvalues[ j ];
989 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
990 if ( !isascii( bvp->bv_val[ k ] )) {
996 printf( _("\tNOT ASCII (%ld bytes)\n"), bvp->bv_len );
998 printf( "\t%s\n", bvp->bv_val );
1006 printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
1008 printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
1014 rc = ldap_add_ext( ld, dn, pmods, pctrls, NULL, &msgid );
1016 rc = ldap_modify_ext( ld, dn, pmods, pctrls, NULL, &msgid );
1019 if ( rc != LDAP_SUCCESS ) {
1020 /* print error message about failed update including DN */
1021 fprintf( stderr, _("%s: update failed: %s\n"), prog, dn );
1022 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
1024 } else if ( verbose ) {
1025 printf( _("modify complete\n") );
1028 rc = process_response( ld, msgid,
1029 newentry ? "ldap_add" : "ldap_modify", dn );
1044 LDAPControl **pctrls )
1049 printf( _("%sdeleting entry \"%s\"\n"), not ? "!" : "", dn );
1051 rc = ldap_delete_ext( ld, dn, pctrls, NULL, &msgid );
1052 if ( rc != LDAP_SUCCESS ) {
1053 fprintf( stderr, _("%s: delete failed: %s\n"), prog, dn );
1054 ldap_perror( ld, "ldap_delete" );
1056 } else if ( verbose ) {
1057 printf( _("delete complete") );
1060 rc = process_response( ld, msgid, "ldap_delete", dn );
1078 LDAPControl **pctrls )
1083 printf( _("%smodifying rdn of entry \"%s\"\n"), not ? "!" : "", dn );
1085 printf( _("\tnew RDN: \"%s\" (%skeep existing values)\n"),
1086 newrdn, deleteoldrdn ? _("do not ") : "" );
1089 rc = ldap_rename( ld, dn, newrdn, newsup, deleteoldrdn,
1090 pctrls, NULL, &msgid );
1091 if ( rc != LDAP_SUCCESS ) {
1092 fprintf( stderr, _("%s: rename failed: %s\n"), prog, dn );
1093 ldap_perror( ld, "ldap_modrdn" );
1096 printf( _("modrdn completed\n") );
1099 rc = process_response( ld, msgid, "ldap_rename", dn );
1110 static int process_response(
1117 int rc = LDAP_OTHER;
1119 if( ldap_result( ld, msgid,
1120 #ifdef LDAP_GROUP_TRANSACTION
1125 NULL, &res ) == -1 ) {
1126 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
1130 if( ldap_msgtype( res ) != LDAP_RES_INTERMEDIATE ) {
1131 return ldap_result2error( ld, res, 1 );
1134 #ifdef LDAP_GROUP_TRANSACTION
1135 /* assume (successful) transaction intermediate response */
1136 return LDAP_SUCCESS;
1139 /* intermediate response? */
1140 return LDAP_DECODING_ERROR;
1145 read_one_record( FILE *fp )
1147 char *buf, line[ LDAPMOD_MAXLINE ];
1153 while ( fgets( line, sizeof(line), fp ) != NULL ) {
1154 int len = strlen( line );
1156 if( len < 2 || ( len == 2 && *line == '\r' )) {
1164 if ( lcur + len + 1 > lmax ) {
1165 lmax = LDAPMOD_MAXLINE
1166 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
1168 if (( buf = (char *)ber_memrealloc( buf, lmax )) == NULL ) {
1169 perror( "realloc" );
1170 exit( EXIT_FAILURE );
1174 strcpy( buf + lcur, line );