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-2007 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 )
322 #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 if ( lutil_atoi( &replicaport, p ) != 0 ) {
471 fprintf( stderr, _("%s: unable to parse replica port \"%s\" (line %d) entry: \"%s\"\n"),
472 prog, p, linenum, dn == NULL ? "" : dn );
473 rc = LDAP_PARAM_ERROR;
477 if ( ldaphost != NULL &&
478 strcasecmp( val.bv_val, ldaphost ) == 0 &&
479 replicaport == ldapport )
483 } else if ( count == 1 && linenum == 1 &&
484 strcasecmp( type, T_VERSION_STR ) == 0 )
487 if( val.bv_len == 0 || lutil_atoi( &v, val.bv_val) != 0 || v != 1 ) {
489 _("%s: invalid version %s, line %d (ignored)\n"),
490 prog, val.bv_val, linenum );
494 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
495 if (( dn = ber_strdup( val.bv_val )) == NULL ) {
497 exit( EXIT_FAILURE );
501 goto end_line; /* skip all lines until we see "dn:" */
505 /* Check for "control" tag after dn and before changetype. */
506 if (strcasecmp(type, T_CONTROL_STR) == 0) {
507 /* Parse and add it to the list of controls */
508 rc = parse_ldif_control( line, &pctrls );
511 _("%s: Error processing %s line, line %d: %s\n"),
512 prog, T_CONTROL_STR, linenum, ldap_err2string(rc) );
518 if ( !use_record && saw_replica ) {
519 printf(_("%s: skipping change record for entry: %s\n"),
521 printf(_("\t(LDAP host/port does not match replica: lines)\n"));
524 ber_memfree( val.bv_val );
528 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
529 #ifdef LIBERAL_CHANGETYPE_MODOP
530 /* trim trailing spaces (and log warning ...) */
532 for ( icnt = val.bv_len; --icnt > 0; ) {
533 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
538 if ( ++icnt != val.bv_len ) {
539 fprintf( stderr, _("%s: illegal trailing space after"
540 " \"%s: %s\" trimmed (line %d of entry \"%s\")\n"),
541 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
542 val.bv_val[icnt] = '\0';
544 #endif /* LIBERAL_CHANGETYPE_MODOP */
546 if ( strcasecmp( val.bv_val, T_MODIFYCTSTR ) == 0 ) {
549 } else if ( strcasecmp( val.bv_val, T_ADDCTSTR ) == 0 ) {
551 } else if ( strcasecmp( val.bv_val, T_MODRDNCTSTR ) == 0
552 || strcasecmp( val.bv_val, T_MODDNCTSTR ) == 0
553 || strcasecmp( val.bv_val, T_RENAMECTSTR ) == 0)
556 } else if ( strcasecmp( val.bv_val, T_DELETECTSTR ) == 0 ) {
557 got_all = delete_entry = 1;
560 _("%s: unknown %s \"%s\" (line %d of entry \"%s\")\n"),
561 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
562 rc = LDAP_PARAM_ERROR;
565 } else if ( ldapadd ) { /* missing changetype => add */
567 modop = LDAP_MOD_ADD;
569 expect_modop = 1; /* missing changetype => modify */
573 if ( expect_modop ) {
574 #ifdef LIBERAL_CHANGETYPE_MODOP
575 /* trim trailing spaces (and log warning ...) */
577 for ( icnt = val.bv_len; --icnt > 0; ) {
578 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) break;
581 if ( ++icnt != val.bv_len ) {
582 fprintf( stderr, _("%s: illegal trailing space after"
583 " \"%s: %s\" trimmed (line %d of entry \"%s\")\n"),
584 prog, type, val.bv_val, linenum, dn );
585 val.bv_val[icnt] = '\0';
587 #endif /* LIBERAL_CHANGETYPE_MODOP */
591 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
592 modop = LDAP_MOD_ADD;
594 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
595 modop = LDAP_MOD_REPLACE;
596 addmodifyop( &pmods, modop, val.bv_val, NULL );
598 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
599 modop = LDAP_MOD_DELETE;
600 addmodifyop( &pmods, modop, val.bv_val, NULL );
602 } else if ( strcasecmp( type, T_MODOPINCREMENTSTR ) == 0 ) {
603 modop = LDAP_MOD_INCREMENT;
604 addmodifyop( &pmods, modop, val.bv_val, NULL );
606 } else { /* no modify op: use default */
607 modop = ldapadd ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
611 if ( expect_newrdn ) {
612 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
613 if (( newrdn = ber_strdup( val.bv_val )) == NULL ) {
615 exit( EXIT_FAILURE );
617 expect_deleteoldrdn = 1;
620 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
621 " \"%s:\" (line %d of entry \"%s\")\n"),
622 prog, T_NEWRDNSTR, type, linenum, dn );
623 rc = LDAP_PARAM_ERROR;
625 } else if ( expect_deleteoldrdn ) {
626 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
627 deleteoldrdn = ( *val.bv_val == '0' ) ? 0 : 1;
628 expect_deleteoldrdn = 0;
632 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
633 " \"%s:\" (line %d of entry \"%s\")\n"),
634 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
635 rc = LDAP_PARAM_ERROR;
637 } else if ( expect_newsup ) {
638 if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
639 if (( newsup = ber_strdup( val.bv_val )) == NULL ) {
641 exit( EXIT_FAILURE );
645 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
646 " \"%s:\" (line %d of entry \"%s\")\n"),
647 prog, T_NEWSUPSTR, type, linenum, dn );
648 rc = LDAP_PARAM_ERROR;
650 } else if ( got_all ) {
652 _("%s: extra lines at end (line %d of entry \"%s\")\n"),
654 rc = LDAP_PARAM_ERROR;
656 if ( new_entry && strcasecmp( type, T_DN_STR ) == 0 ) {
657 fprintf( stderr, _("%s: attributeDescription \"%s\":"
658 " (possible missing newline"
659 " after line %d of entry \"%s\"?)\n"),
660 prog, type, linenum - 1, dn );
662 addmodifyop( &pmods, modop, type, &val );
667 ber_memfree( val.bv_val );
674 if( version && linenum == 1 ) {
678 /* If default controls are set (as with -M option) and controls are
679 specified in the LDIF file, we must add the default controls to
680 the list of controls sent with the ldap operation.
684 LDAPControl **defctrls = NULL; /* Default server controls */
685 LDAPControl **newctrls = NULL;
686 ldap_get_option(ld, LDAP_OPT_SERVER_CONTROLS, &defctrls);
688 int npc=0; /* Num of LDIF controls */
689 int ndefc=0; /* Num of default controls */
690 while (pctrls[npc]) npc++; /* Count LDIF controls */
691 while (defctrls[ndefc]) ndefc++; /* Count default controls */
692 newctrls = ber_memrealloc(pctrls,
693 (npc+ndefc+1)*sizeof(LDAPControl*));
695 if (newctrls == NULL) {
700 for (i=npc; i<npc+ndefc; i++) {
701 pctrls[i] = ldap_control_dup(defctrls[i-npc]);
702 if (pctrls[i] == NULL) {
707 pctrls[npc+ndefc] = NULL;
709 ldap_controls_free(defctrls); /* Must be freed by library */
716 if ( delete_entry ) {
717 rc = dodelete( dn, pctrls );
718 } else if ( newrdn != NULL ) {
719 rc = dorename( dn, newrdn, newsup, deleteoldrdn, pctrls );
721 rc = domodify( dn, pmods, pctrls, new_entry );
724 if ( rc == LDAP_SUCCESS ) {
732 if ( newrdn != NULL ) {
735 if ( newsup != NULL ) {
738 if ( pmods != NULL ) {
739 ldap_mods_free( pmods, 1 );
741 if (pctrls != NULL) {
742 ldap_controls_free( pctrls );
748 /* Parse an LDIF control line of the form
749 control: oid [true/false] [: value] or
750 control: oid [true/false] [:: base64-value] or
751 control: oid [true/false] [:< url]
752 The control is added to the list of controls in *ppctrls.
757 LDAPControl ***ppctrls )
760 int criticality = 0; /* Default is false if not present */
763 ber_len_t value_len = 0;
765 char *s, *oidStart, *pcolon;
766 LDAPControl *newctrl = NULL;
767 LDAPControl **pctrls = NULL;
769 if (ppctrls) pctrls = *ppctrls;
770 s = line + strlen(T_CONTROL_STR); /* Skip over "control" */
771 pcolon = s; /* Save this position for later */
772 if (*s++ != ':') { /* Make sure colon follows */
773 return ( LDAP_PARAM_ERROR );
775 while (*s && isspace((unsigned char)*s)) {
776 s++; /* Skip white space before OID */
779 /* OID should come next. Validate and extract it. */
780 if (*s == 0) return ( LDAP_PARAM_ERROR );
782 while (isdigit((unsigned char)*s) || *s == '.') {
783 s++; /* OID should be digits or . */
786 return ( LDAP_PARAM_ERROR ); /* OID was not present */
788 if (*s) { /* End of OID should be space or NULL */
789 if (!isspace((unsigned char)*s)) {
790 return ( LDAP_PARAM_ERROR ); /* else OID contained invalid chars */
792 *s++ = 0; /* Replace space with null to terminate */
795 oid = ber_strdup(oidStart);
796 if (oid == NULL) return ( LDAP_NO_MEMORY );
798 /* Optional Criticality field is next. */
799 while (*s && isspace((unsigned char)*s)) {
800 s++; /* Skip white space before criticality */
802 if (strncasecmp(s, "true", 4) == 0) {
806 else if (strncasecmp(s, "false", 5) == 0) {
811 /* Optional value field is next */
812 while (*s && isspace((unsigned char)*s)) {
813 s++; /* Skip white space before value */
816 if (*s != ':') { /* If value is present, must start with : */
817 rc = LDAP_PARAM_ERROR;
821 /* Shift value down over OID and criticality so it's in the form
823 control:: base64-value
825 Then we can use ldif_parse_line to extract and decode the value
827 while ( (*pcolon++ = *s++) != 0) { /* Shift value */
830 rc = ldif_parse_line(line, &type, &val, &value_len);
831 if (type) ber_memfree(type); /* Don't need this field*/
833 rc = LDAP_PARAM_ERROR;
838 /* Create a new LDAPControl structure. */
839 newctrl = (LDAPControl *)ber_memalloc(sizeof(LDAPControl));
840 if ( newctrl == NULL ) {
844 newctrl->ldctl_oid = oid;
846 newctrl->ldctl_iscritical = criticality;
847 newctrl->ldctl_value.bv_len = value_len;
848 newctrl->ldctl_value.bv_val = val;
851 /* Add the new control to the passed-in list of controls. */
854 while ( pctrls[i] ) { /* Count the # of controls passed in */
858 /* Allocate 1 more slot for the new control and 1 for the NULL. */
859 pctrls = (LDAPControl **) ber_memrealloc(pctrls,
860 (i+2)*(sizeof(LDAPControl *)));
861 if (pctrls == NULL) {
872 if (newctrl->ldctl_oid) ber_memfree(newctrl->ldctl_oid);
873 if (newctrl->ldctl_value.bv_val) {
874 ber_memfree(newctrl->ldctl_value.bv_val);
876 ber_memfree(newctrl);
878 if (val) ber_memfree(val);
879 if (oid) ber_memfree(oid);
896 modop |= LDAP_MOD_BVALUES;
899 if ( pmods != NULL ) {
900 for ( ; pmods[ i ] != NULL; ++i ) {
901 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
902 pmods[ i ]->mod_op == modop )
909 if ( pmods == NULL || pmods[ i ] == NULL ) {
910 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
911 sizeof( LDAPMod * ))) == NULL )
914 exit( EXIT_FAILURE );
918 pmods[ i + 1 ] = NULL;
920 pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod ));
921 if ( pmods[ i ] == NULL ) {
923 exit( EXIT_FAILURE );
926 pmods[ i ]->mod_op = modop;
927 pmods[ i ]->mod_type = ber_strdup( attr );
928 if ( pmods[ i ]->mod_type == NULL ) {
930 exit( EXIT_FAILURE );
936 if ( pmods[ i ]->mod_bvalues != NULL ) {
937 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
942 pmods[ i ]->mod_bvalues = (struct berval **) ber_memrealloc(
943 pmods[ i ]->mod_bvalues, (j + 2) * sizeof( struct berval * ));
944 if ( pmods[ i ]->mod_bvalues == NULL ) {
945 perror( "ber_realloc" );
946 exit( EXIT_FAILURE );
949 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
950 pmods[ i ]->mod_bvalues[ j ] = ber_bvdup( val );
951 if ( pmods[ i ]->mod_bvalues[ j ] == NULL ) {
952 perror( "ber_bvdup" );
953 exit( EXIT_FAILURE );
963 LDAPControl **pctrls,
966 int rc, i, j, k, notascii, op;
970 fprintf( stderr, _("%s: no DN specified\n"), prog );
971 return( LDAP_PARAM_ERROR );
974 if ( pmods == NULL ) {
975 /* implement "touch" (empty sequence)
976 * modify operation (note that there
977 * is no symmetry with the UNIX command,
978 * since \"touch\" on a non-existent entry
980 printf( "warning: no attributes to %sadd (entry=\"%s\")\n",
981 newentry ? "" : "change or ", dn );
984 for ( i = 0; pmods[ i ] != NULL; ++i ) {
985 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
986 if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
988 _("%s: attribute \"%s\" has no values (entry=\"%s\")\n"),
989 prog, pmods[i]->mod_type, dn );
990 return LDAP_PARAM_ERROR;
995 for ( i = 0; pmods[ i ] != NULL; ++i ) {
996 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
998 op == LDAP_MOD_REPLACE ? _("replace") :
999 op == LDAP_MOD_ADD ? _("add") :
1000 op == LDAP_MOD_INCREMENT ? _("increment") :
1001 op == LDAP_MOD_DELETE ? _("delete") :
1003 pmods[ i ]->mod_type );
1005 if ( pmods[ i ]->mod_bvalues != NULL ) {
1006 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1007 bvp = pmods[ i ]->mod_bvalues[ j ];
1009 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
1010 if ( !isascii( bvp->bv_val[ k ] )) {
1016 printf( _("\tNOT ASCII (%ld bytes)\n"), bvp->bv_len );
1018 printf( "\t%s\n", bvp->bv_val );
1027 printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
1029 printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
1035 rc = ldap_add_ext( ld, dn, pmods, pctrls, NULL, &msgid );
1037 rc = ldap_modify_ext( ld, dn, pmods, pctrls, NULL, &msgid );
1040 if ( rc != LDAP_SUCCESS ) {
1041 /* print error message about failed update including DN */
1042 fprintf( stderr, _("%s: update failed: %s\n"), prog, dn );
1043 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
1045 } else if ( verbose ) {
1046 printf( _("modify complete\n") );
1049 rc = process_response( ld, msgid,
1050 newentry ? "ldap_add" : "ldap_modify", dn );
1065 LDAPControl **pctrls )
1070 printf( _("%sdeleting entry \"%s\"\n"), not ? "!" : "", dn );
1072 rc = ldap_delete_ext( ld, dn, pctrls, NULL, &msgid );
1073 if ( rc != LDAP_SUCCESS ) {
1074 fprintf( stderr, _("%s: delete failed: %s\n"), prog, dn );
1075 ldap_perror( ld, "ldap_delete" );
1077 } else if ( verbose ) {
1078 printf( _("delete complete") );
1081 rc = process_response( ld, msgid, "ldap_delete", dn );
1099 LDAPControl **pctrls )
1104 printf( _("%smodifying rdn of entry \"%s\"\n"), not ? "!" : "", dn );
1106 printf( _("\tnew RDN: \"%s\" (%skeep existing values)\n"),
1107 newrdn, deleteoldrdn ? _("do not ") : "" );
1110 rc = ldap_rename( ld, dn, newrdn, newsup, deleteoldrdn,
1111 pctrls, NULL, &msgid );
1112 if ( rc != LDAP_SUCCESS ) {
1113 fprintf( stderr, _("%s: rename failed: %s\n"), prog, dn );
1114 ldap_perror( ld, "ldap_rename" );
1117 printf( _("rename completed\n") );
1120 rc = process_response( ld, msgid, "ldap_rename", dn );
1131 static int process_response(
1138 int rc = LDAP_OTHER;
1139 struct timeval tv = { 0, 0 };
1143 tv.tv_usec = 100000;
1145 rc = ldap_result( ld, msgid,
1146 #ifdef LDAP_GROUP_TRANSACTION
1152 if ( tool_check_abandon( ld, msgid ) ) {
1153 return LDAP_CANCELLED;
1157 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
1158 tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
1167 if ( ldap_msgtype( res ) != LDAP_RES_INTERMEDIATE ) {
1168 rc = ldap_result2error( ld, res, 1 );
1169 if( rc != LDAP_SUCCESS ) ldap_perror( ld, opstr );
1173 #ifdef LDAP_GROUP_TRANSACTION
1174 /* assume (successful) transaction intermediate response */
1175 return LDAP_SUCCESS;
1178 /* intermediate response? */
1179 return LDAP_DECODING_ERROR;
1184 read_one_record( FILE *fp )
1186 char *buf, line[ LDAPMOD_MAXLINE ];
1192 while ( fgets( line, sizeof(line), fp ) != NULL ) {
1193 int len = strlen( line );
1195 if( len < 2 || ( len == 2 && *line == '\r' )) {
1203 if ( lcur + len + 1 > lmax ) {
1204 lmax = LDAPMOD_MAXLINE
1205 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
1207 if (( buf = (char *)ber_memrealloc( buf, lmax )) == NULL ) {
1208 perror( "realloc" );
1209 exit( EXIT_FAILURE );
1213 strcpy( buf + lcur, line );