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-2006 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;
257 prog = lutil_progname( "ldapmodify", argc, argv );
259 /* strncmp instead of strcmp since NT binaries carry .exe extension */
260 ldapadd = ( strncasecmp( prog, "ldapadd", sizeof("ldapadd")-1 ) == 0 );
262 tool_init( ldapadd ? TOOL_ADD : TOOL_MODIFY );
264 tool_args( argc, argv );
266 if ( argc != optind ) usage();
268 if ( rejfile != NULL ) {
269 if (( rejfp = fopen( rejfile, "w" )) == NULL ) {
271 return( EXIT_FAILURE );
277 if ( infile != NULL ) {
278 if (( fp = fopen( infile, "r" )) == NULL ) {
280 return( EXIT_FAILURE );
286 if ( debug ) ldif_debug = debug;
288 ld = tool_conn_setup( dont, 0 );
291 if ( pw_file || want_bindpw ) {
293 rc = lutil_get_filed_password( pw_file, &passwd );
294 if( rc ) return EXIT_FAILURE;
296 passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
297 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
303 #ifdef LDAP_GROUP_TRANSACTION
305 struct berval *txnCookiep = &txnCookie;
307 /* create transaction */
308 rc = ldap_txn_create_s( ld, &txnCookiep, NULL, NULL );
309 if( rc != LDAP_SUCCESS ) {
310 tool_perror( "ldap_txn_create_s", rc, NULL, NULL, NULL, NULL );
311 if( txn > 2 ) return EXIT_FAILURE;
318 #ifdef LDAP_GROUP_TRANSACTION
323 #ifdef LDAP_GROUP_TRANSACTION
326 txnber = ber_alloc_t( LBER_USE_DER );
327 if( txnber == NULL ) return EXIT_FAILURE;
329 err = ber_printf( txnber, "{o}", &txnCookie );
331 ber_free( txnber, 1 );
332 fprintf( stderr, _("txn grouping control encoding error!\n") );
336 err = ber_flatten2( txnber, &c[i].ldctl_value, 0 );
337 if( err == -1 ) return EXIT_FAILURE;
339 c[i].ldctl_oid = LDAP_CONTROL_GROUPING;
340 c[i].ldctl_iscritical = 1;
346 tool_server_controls( ld, c, i );
351 while (( rc == 0 || contoper ) &&
352 ( rbuf = read_one_record( fp )) != NULL )
359 len = strlen( rbuf );
360 if (( rejbuf = (char *)ber_memalloc( len+1 )) == NULL ) {
362 exit( EXIT_FAILURE );
364 memcpy( rejbuf, rbuf, len+1 );
367 rc = process_ldif_rec( start, count );
369 if ( rc ) retval = rc;
371 fprintf(rejfp, _("# Error: %s (%d)"), ldap_err2string(rc), rc);
374 ldap_get_option(ld, LDAP_OPT_MATCHED_DN, &matched_msg);
375 if ( matched_msg != NULL ) {
376 if ( *matched_msg != '\0' ) {
377 fprintf( rejfp, _(", matched DN: %s"), matched_msg );
379 ldap_memfree( matched_msg );
383 ldap_get_option(ld, LDAP_OPT_ERROR_STRING, &error_msg);
384 if ( error_msg != NULL ) {
385 if ( *error_msg != '\0' ) {
386 fprintf( rejfp, _(", additional info: %s"), error_msg );
388 ldap_memfree( error_msg );
390 fprintf( rejfp, "\n%s\n", rejbuf );
393 if (rejfp) free( rejbuf );
397 #ifdef LDAP_GROUP_TRANSACTION
399 /* create transaction */
400 rc = ldap_txn_end_s( ld, &txnCookie, !txnabort, NULL, NULL );
401 if( rc != LDAP_SUCCESS ) {
402 tool_perror( "ldap_txn_create_s", rc, NULL, NULL, NULL, NULL );
403 if( txn > 2 ) return EXIT_FAILURE;
413 if ( rejfp != NULL ) {
423 process_ldif_rec( char *rbuf, int count )
425 char *line, *dn, *type, *newrdn, *newsup, *p;
426 int rc, linenum, modop, replicaport;
427 int expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
428 int expect_deleteoldrdn, deleteoldrdn;
429 int saw_replica, use_record, new_entry, delete_entry, got_all;
433 LDAPControl **pctrls;
437 rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
438 expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
439 expect_sep = expect_ct = 0;
446 dn = newrdn = newsup = NULL;
448 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
451 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
457 if ( ldif_parse_line( line, &type, &val.bv_val, &val.bv_len ) < 0 ) {
458 fprintf( stderr, _("%s: invalid format (line %d) entry: \"%s\"\n"),
459 prog, linenum, dn == NULL ? "" : dn );
460 rc = LDAP_PARAM_ERROR;
465 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
467 if (( p = strchr( val.bv_val, ':' )) == NULL ) {
471 if ( lutil_atoi( &replicaport, p ) != 0 ) {
472 fprintf( stderr, _("%s: unable to parse replica port \"%s\" (line %d) entry: \"%s\"\n"),
473 prog, p, linenum, dn == NULL ? "" : dn );
474 rc = LDAP_PARAM_ERROR;
478 if ( ldaphost != NULL &&
479 strcasecmp( val.bv_val, ldaphost ) == 0 &&
480 replicaport == ldapport )
484 } else if ( count == 1 && linenum == 1 &&
485 strcasecmp( type, T_VERSION_STR ) == 0 )
488 if( val.bv_len == 0 || lutil_atoi( &v, val.bv_val) != 0 || v != 1 ) {
490 _("%s: invalid version %s, line %d (ignored)\n"),
491 prog, val.bv_val, linenum );
495 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
496 if (( dn = ber_strdup( val.bv_val )) == NULL ) {
498 exit( EXIT_FAILURE );
502 goto end_line; /* skip all lines until we see "dn:" */
506 /* Check for "control" tag after dn and before changetype. */
507 if (strcasecmp(type, T_CONTROL_STR) == 0) {
508 /* Parse and add it to the list of controls */
509 rc = parse_ldif_control( line, &pctrls );
512 _("%s: Error processing %s line, line %d: %s\n"),
513 prog, T_CONTROL_STR, linenum, ldap_err2string(rc) );
519 if ( !use_record && saw_replica ) {
520 printf(_("%s: skipping change record for entry: %s\n"),
522 printf(_("\t(LDAP host/port does not match replica: lines)\n"));
525 ber_memfree( val.bv_val );
529 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
530 #ifdef LIBERAL_CHANGETYPE_MODOP
531 /* trim trailing spaces (and log warning ...) */
533 for ( icnt = val.bv_len; --icnt > 0; ) {
534 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
539 if ( ++icnt != val.bv_len ) {
540 fprintf( stderr, _("%s: illegal trailing space after"
541 " \"%s: %s\" trimmed (line %d of entry \"%s\")\n"),
542 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
543 val.bv_val[icnt] = '\0';
545 #endif /* LIBERAL_CHANGETYPE_MODOP */
547 if ( strcasecmp( val.bv_val, T_MODIFYCTSTR ) == 0 ) {
550 } else if ( strcasecmp( val.bv_val, T_ADDCTSTR ) == 0 ) {
552 } else if ( strcasecmp( val.bv_val, T_MODRDNCTSTR ) == 0
553 || strcasecmp( val.bv_val, T_MODDNCTSTR ) == 0
554 || strcasecmp( val.bv_val, T_RENAMECTSTR ) == 0)
557 } else if ( strcasecmp( val.bv_val, T_DELETECTSTR ) == 0 ) {
558 got_all = delete_entry = 1;
561 _("%s: unknown %s \"%s\" (line %d of entry \"%s\")\n"),
562 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
563 rc = LDAP_PARAM_ERROR;
566 } else if ( ldapadd ) { /* missing changetype => add */
568 modop = LDAP_MOD_ADD;
570 expect_modop = 1; /* missing changetype => modify */
574 if ( expect_modop ) {
575 #ifdef LIBERAL_CHANGETYPE_MODOP
576 /* trim trailing spaces (and log warning ...) */
578 for ( icnt = val.bv_len; --icnt > 0; ) {
579 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) break;
582 if ( ++icnt != val.bv_len ) {
583 fprintf( stderr, _("%s: illegal trailing space after"
584 " \"%s: %s\" trimmed (line %d of entry \"%s\")\n"),
585 prog, type, val.bv_val, linenum, dn );
586 val.bv_val[icnt] = '\0';
588 #endif /* LIBERAL_CHANGETYPE_MODOP */
592 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
593 modop = LDAP_MOD_ADD;
595 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
596 modop = LDAP_MOD_REPLACE;
597 addmodifyop( &pmods, modop, val.bv_val, NULL );
599 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
600 modop = LDAP_MOD_DELETE;
601 addmodifyop( &pmods, modop, val.bv_val, NULL );
603 } else if ( strcasecmp( type, T_MODOPINCREMENTSTR ) == 0 ) {
604 modop = LDAP_MOD_INCREMENT;
605 addmodifyop( &pmods, modop, val.bv_val, NULL );
607 } else { /* no modify op: use default */
608 modop = ldapadd ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
612 if ( expect_newrdn ) {
613 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
614 if (( newrdn = ber_strdup( val.bv_val )) == NULL ) {
616 exit( EXIT_FAILURE );
618 expect_deleteoldrdn = 1;
621 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
622 " \"%s:\" (line %d of entry \"%s\")\n"),
623 prog, T_NEWRDNSTR, type, linenum, dn );
624 rc = LDAP_PARAM_ERROR;
626 } else if ( expect_deleteoldrdn ) {
627 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
628 deleteoldrdn = ( *val.bv_val == '0' ) ? 0 : 1;
629 expect_deleteoldrdn = 0;
633 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
634 " \"%s:\" (line %d of entry \"%s\")\n"),
635 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
636 rc = LDAP_PARAM_ERROR;
638 } else if ( expect_newsup ) {
639 if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
640 if (( newsup = ber_strdup( val.bv_val )) == NULL ) {
642 exit( EXIT_FAILURE );
646 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
647 " \"%s:\" (line %d of entry \"%s\")\n"),
648 prog, T_NEWSUPSTR, type, linenum, dn );
649 rc = LDAP_PARAM_ERROR;
651 } else if ( got_all ) {
653 _("%s: extra lines at end (line %d of entry \"%s\")\n"),
655 rc = LDAP_PARAM_ERROR;
657 if ( new_entry && strcasecmp( type, T_DN_STR ) == 0 ) {
658 fprintf( stderr, _("%s: attributeDescription \"%s\":"
659 " (possible missing newline"
660 " after line %d of entry \"%s\"?)\n"),
661 prog, type, linenum - 1, dn );
663 addmodifyop( &pmods, modop, type, &val );
668 ber_memfree( val.bv_val );
675 if( version && linenum == 1 ) {
679 /* If default controls are set (as with -M option) and controls are
680 specified in the LDIF file, we must add the default controls to
681 the list of controls sent with the ldap operation.
685 LDAPControl **defctrls = NULL; /* Default server controls */
686 LDAPControl **newctrls = NULL;
687 ldap_get_option(ld, LDAP_OPT_SERVER_CONTROLS, &defctrls);
689 int npc=0; /* Num of LDIF controls */
690 int ndefc=0; /* Num of default controls */
691 while (pctrls[npc]) npc++; /* Count LDIF controls */
692 while (defctrls[ndefc]) ndefc++; /* Count default controls */
693 newctrls = ber_memrealloc(pctrls,
694 (npc+ndefc+1)*sizeof(LDAPControl*));
696 if (newctrls == NULL) {
701 for (i=npc; i<npc+ndefc; i++) {
702 pctrls[i] = ldap_control_dup(defctrls[i-npc]);
703 if (pctrls[i] == NULL) {
708 pctrls[npc+ndefc] = NULL;
710 ldap_controls_free(defctrls); /* Must be freed by library */
717 if ( delete_entry ) {
718 rc = dodelete( dn, pctrls );
719 } else if ( newrdn != NULL ) {
720 rc = dorename( dn, newrdn, newsup, deleteoldrdn, pctrls );
722 rc = domodify( dn, pmods, pctrls, new_entry );
725 if ( rc == LDAP_SUCCESS ) {
733 if ( newrdn != NULL ) {
736 if ( newsup != NULL ) {
739 if ( pmods != NULL ) {
740 ldap_mods_free( pmods, 1 );
742 if (pctrls != NULL) {
743 ldap_controls_free( pctrls );
749 /* Parse an LDIF control line of the form
750 control: oid [true/false] [: value] or
751 control: oid [true/false] [:: base64-value] or
752 control: oid [true/false] [:< url]
753 The control is added to the list of controls in *ppctrls.
758 LDAPControl ***ppctrls )
761 int criticality = 0; /* Default is false if not present */
764 ber_len_t value_len = 0;
766 char *s, *oidStart, *pcolon;
767 LDAPControl *newctrl = NULL;
768 LDAPControl **pctrls = NULL;
770 if (ppctrls) pctrls = *ppctrls;
771 s = line + strlen(T_CONTROL_STR); /* Skip over "control" */
772 pcolon = s; /* Save this position for later */
773 if (*s++ != ':') { /* Make sure colon follows */
774 return ( LDAP_PARAM_ERROR );
776 while (*s && isspace((unsigned char)*s)) {
777 s++; /* Skip white space before OID */
780 /* OID should come next. Validate and extract it. */
781 if (*s == 0) return ( LDAP_PARAM_ERROR );
783 while (isdigit((unsigned char)*s) || *s == '.') {
784 s++; /* OID should be digits or . */
787 return ( LDAP_PARAM_ERROR ); /* OID was not present */
789 if (*s) { /* End of OID should be space or NULL */
790 if (!isspace((unsigned char)*s)) {
791 return ( LDAP_PARAM_ERROR ); /* else OID contained invalid chars */
793 *s++ = 0; /* Replace space with null to terminate */
796 oid = ber_strdup(oidStart);
797 if (oid == NULL) return ( LDAP_NO_MEMORY );
799 /* Optional Criticality field is next. */
800 while (*s && isspace((unsigned char)*s)) {
801 s++; /* Skip white space before criticality */
803 if (strncasecmp(s, "true", 4) == 0) {
807 else if (strncasecmp(s, "false", 5) == 0) {
812 /* Optional value field is next */
813 while (*s && isspace((unsigned char)*s)) {
814 s++; /* Skip white space before value */
817 if (*s != ':') { /* If value is present, must start with : */
818 rc = LDAP_PARAM_ERROR;
822 /* Shift value down over OID and criticality so it's in the form
824 control:: base64-value
826 Then we can use ldif_parse_line to extract and decode the value
828 while ( (*pcolon++ = *s++) != 0) { /* Shift value */
831 rc = ldif_parse_line(line, &type, &val, &value_len);
832 if (type) ber_memfree(type); /* Don't need this field*/
834 rc = LDAP_PARAM_ERROR;
839 /* Create a new LDAPControl structure. */
840 newctrl = (LDAPControl *)ber_memalloc(sizeof(LDAPControl));
841 if ( newctrl == NULL ) {
845 newctrl->ldctl_oid = oid;
847 newctrl->ldctl_iscritical = criticality;
848 newctrl->ldctl_value.bv_len = value_len;
849 newctrl->ldctl_value.bv_val = val;
852 /* Add the new control to the passed-in list of controls. */
855 while ( pctrls[i] ) { /* Count the # of controls passed in */
859 /* Allocate 1 more slot for the new control and 1 for the NULL. */
860 pctrls = (LDAPControl **) ber_memrealloc(pctrls,
861 (i+2)*(sizeof(LDAPControl *)));
862 if (pctrls == NULL) {
873 if (newctrl->ldctl_oid) ber_memfree(newctrl->ldctl_oid);
874 if (newctrl->ldctl_value.bv_val) {
875 ber_memfree(newctrl->ldctl_value.bv_val);
877 ber_memfree(newctrl);
879 if (val) ber_memfree(val);
880 if (oid) ber_memfree(oid);
897 modop |= LDAP_MOD_BVALUES;
900 if ( pmods != NULL ) {
901 for ( ; pmods[ i ] != NULL; ++i ) {
902 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
903 pmods[ i ]->mod_op == modop )
910 if ( pmods == NULL || pmods[ i ] == NULL ) {
911 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
912 sizeof( LDAPMod * ))) == NULL )
915 exit( EXIT_FAILURE );
919 pmods[ i + 1 ] = NULL;
921 pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod ));
922 if ( pmods[ i ] == NULL ) {
924 exit( EXIT_FAILURE );
927 pmods[ i ]->mod_op = modop;
928 pmods[ i ]->mod_type = ber_strdup( attr );
929 if ( pmods[ i ]->mod_type == NULL ) {
931 exit( EXIT_FAILURE );
937 if ( pmods[ i ]->mod_bvalues != NULL ) {
938 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
943 pmods[ i ]->mod_bvalues = (struct berval **) ber_memrealloc(
944 pmods[ i ]->mod_bvalues, (j + 2) * sizeof( struct berval * ));
945 if ( pmods[ i ]->mod_bvalues == NULL ) {
946 perror( "ber_realloc" );
947 exit( EXIT_FAILURE );
950 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
951 pmods[ i ]->mod_bvalues[ j ] = ber_bvdup( val );
952 if ( pmods[ i ]->mod_bvalues[ j ] == NULL ) {
953 perror( "ber_bvdup" );
954 exit( EXIT_FAILURE );
964 LDAPControl **pctrls,
967 int rc, i, j, k, notascii, op;
971 fprintf( stderr, _("%s: no DN specified\n"), prog );
972 return( LDAP_PARAM_ERROR );
975 if ( pmods == NULL ) {
976 /* implement "touch" (empty sequence)
977 * modify operation (note that there
978 * is no symmetry with the UNIX command,
979 * since \"touch\" on a non-existent entry
981 printf( "warning: no attributes to %sadd (entry=\"%s\")\n",
982 newentry ? "" : "change or ", dn );
985 for ( i = 0; pmods[ i ] != NULL; ++i ) {
986 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
987 if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
989 _("%s: attribute \"%s\" has no values (entry=\"%s\")\n"),
990 prog, pmods[i]->mod_type, dn );
991 return LDAP_PARAM_ERROR;
996 for ( i = 0; pmods[ i ] != NULL; ++i ) {
997 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
999 op == LDAP_MOD_REPLACE ? _("replace") :
1000 op == LDAP_MOD_ADD ? _("add") :
1001 op == LDAP_MOD_INCREMENT ? _("increment") :
1002 op == LDAP_MOD_DELETE ? _("delete") :
1004 pmods[ i ]->mod_type );
1006 if ( pmods[ i ]->mod_bvalues != NULL ) {
1007 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1008 bvp = pmods[ i ]->mod_bvalues[ j ];
1010 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
1011 if ( !isascii( bvp->bv_val[ k ] )) {
1017 printf( _("\tNOT ASCII (%ld bytes)\n"), bvp->bv_len );
1019 printf( "\t%s\n", bvp->bv_val );
1028 printf( "%sadding new entry \"%s\"\n", dont ? "!" : "", dn );
1030 printf( "%smodifying entry \"%s\"\n", dont ? "!" : "", dn );
1036 rc = ldap_add_ext( ld, dn, pmods, pctrls, NULL, &msgid );
1038 rc = ldap_modify_ext( ld, dn, pmods, pctrls, NULL, &msgid );
1041 if ( rc != LDAP_SUCCESS ) {
1042 /* print error message about failed update including DN */
1043 fprintf( stderr, _("%s: update failed: %s\n"), prog, dn );
1044 tool_perror( newentry ? "ldap_add" : "ldap_modify", rc, NULL, NULL, NULL, NULL );
1046 } else if ( verbose ) {
1047 printf( _("modify complete\n") );
1050 rc = process_response( ld, msgid,
1051 newentry ? LDAP_RES_ADD : LDAP_RES_MODIFY, dn );
1066 LDAPControl **pctrls )
1071 printf( _("%sdeleting entry \"%s\"\n"), dont ? "!" : "", dn );
1073 rc = ldap_delete_ext( ld, dn, pctrls, NULL, &msgid );
1074 if ( rc != LDAP_SUCCESS ) {
1075 fprintf( stderr, _("%s: delete failed: %s\n"), prog, dn );
1076 tool_perror( "ldap_delete", rc, NULL, NULL, NULL, NULL );
1078 } else if ( verbose ) {
1079 printf( _("delete complete") );
1082 rc = process_response( ld, msgid, LDAP_RES_DELETE, dn );
1100 LDAPControl **pctrls )
1105 printf( _("%smodifying rdn of entry \"%s\"\n"), dont ? "!" : "", dn );
1107 printf( _("\tnew RDN: \"%s\" (%skeep existing values)\n"),
1108 newrdn, deleteoldrdn ? _("do not ") : "" );
1111 rc = ldap_rename( ld, dn, newrdn, newsup, deleteoldrdn,
1112 pctrls, NULL, &msgid );
1113 if ( rc != LDAP_SUCCESS ) {
1114 fprintf( stderr, _("%s: rename failed: %s\n"), prog, dn );
1115 tool_perror( "ldap_modrdn", rc, NULL, NULL, NULL, NULL );
1118 printf( _("modrdn completed\n") );
1121 rc = process_response( ld, msgid, LDAP_RES_RENAME, dn );
1133 res2str( int res ) {
1137 case LDAP_RES_DELETE:
1138 return "ldap_delete";
1139 case LDAP_RES_MODIFY:
1140 return "ldap_modify";
1141 case LDAP_RES_MODRDN:
1142 return "ldap_modrdn";
1147 return "ldap_unknown";
1150 static int process_response(
1157 int rc = LDAP_OTHER,
1159 struct timeval tv = { 0, 0 };
1163 tv.tv_usec = 100000;
1165 rc = ldap_result( ld, msgid,
1166 #ifdef LDAP_GROUP_TRANSACTION
1167 txn ? LDAP_MSG_ONE : LDAP_MSG_ALL,
1172 if ( tool_check_abandon( ld, msgid ) ) {
1173 return LDAP_CANCELLED;
1177 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
1186 msgtype = ldap_msgtype( res );
1187 if ( msgtype != LDAP_RES_INTERMEDIATE ) {
1189 char *text = NULL, *matched = NULL, **refs = NULL;
1190 LDAPControl **ctrls = NULL;
1192 rc = ldap_parse_result( ld, res, &err, &matched, &text, &refs, &ctrls, 1 );
1193 if ( rc == LDAP_SUCCESS ) {
1196 if ( rc != LDAP_SUCCESS ) {
1197 tool_perror( res2str( op ), rc, NULL, matched, text, refs );
1198 } else if ( msgtype != op ) {
1199 fprintf( stderr, "%s: msgtype: expected %d got %d\n",
1200 res2str( op ), op, msgtype );
1204 ldap_memfree( text );
1207 ldap_memfree( matched );
1210 ber_memvfree( (void **)refs );
1212 if ( ctrls != NULL ) {
1213 tool_print_ctrls( ld, ctrls );
1214 ldap_controls_free( ctrls );
1219 #ifdef LDAP_GROUP_TRANSACTION
1220 /* assume (successful) transaction intermediate response */
1221 return LDAP_SUCCESS;
1224 /* intermediate response? */
1225 return LDAP_DECODING_ERROR;
1230 read_one_record( FILE *fp )
1232 char *buf, line[ LDAPMOD_MAXLINE ];
1238 while ( fgets( line, sizeof(line), fp ) != NULL ) {
1239 int len = strlen( line );
1241 if( len < 2 || ( len == 2 && *line == '\r' )) {
1249 if ( lcur + len + 1 > lmax ) {
1250 lmax = LDAPMOD_MAXLINE
1251 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
1253 if (( buf = (char *)ber_memrealloc( buf, lmax )) == NULL ) {
1254 perror( "realloc" );
1255 exit( EXIT_FAILURE );
1259 strcpy( buf + lcur, line );