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-2011 The OpenLDAP Foundation.
6 * Portions Copyright 2006 Howard Chu.
7 * Portions Copyright 1998-2003 Kurt D. Zeilenga.
8 * Portions Copyright 1998-2001 Net Boolean Incorporated.
9 * Portions Copyright 2001-2003 IBM Corporation.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted only as authorized by the OpenLDAP
16 * A copy of this license is available in the file LICENSE in the
17 * top-level directory of the distribution or, alternatively, at
18 * <http://www.OpenLDAP.org/license.html>.
20 /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
21 * All rights reserved.
23 * Redistribution and use in source and binary forms are permitted
24 * provided that this notice is preserved and that due credit is given
25 * to the University of Michigan at Ann Arbor. The name of the
26 * University may not be used to endorse or promote products derived
27 * from this software without specific prior written permission. This
28 * software is provided ``as is'' without express or implied warranty.
31 * This work was originally developed by the University of Michigan
32 * (as part of U-MICH LDAP). Additional significant contributors
43 #include <ac/stdlib.h>
45 #include <ac/string.h>
46 #include <ac/unistd.h>
47 #include <ac/socket.h>
50 #ifdef HAVE_SYS_STAT_H
54 #ifdef HAVE_SYS_FILE_H
64 #include "lutil_ldap.h"
66 #include "ldap_defaults.h"
73 static char *rejfile = NULL;
74 static LDAP *ld = NULL;
78 /* strings found in LDIF entries */
79 static struct berval BV_VERSION = BER_BVC("version");
80 static struct berval BV_DN = BER_BVC("dn");
81 static struct berval BV_CONTROL = BER_BVC("control");
82 static struct berval BV_CHANGETYPE = BER_BVC("changetype");
83 static struct berval BV_ADDCT = BER_BVC("add");
84 static struct berval BV_MODIFYCT = BER_BVC("modify");
85 static struct berval BV_DELETECT = BER_BVC("delete");
86 static struct berval BV_MODRDNCT = BER_BVC("modrdn");
87 static struct berval BV_MODDNCT = BER_BVC("moddn");
88 static struct berval BV_RENAMECT = BER_BVC("rename");
89 static struct berval BV_MODOPADD = BER_BVC("add");
90 static struct berval BV_MODOPREPLACE = BER_BVC("replace");
91 static struct berval BV_MODOPDELETE = BER_BVC("delete");
92 static struct berval BV_MODOPINCREMENT = BER_BVC("increment");
93 static struct berval BV_NEWRDN = BER_BVC("newrdn");
94 static struct berval BV_DELETEOLDRDN = BER_BVC("deleteoldrdn");
95 static struct berval BV_NEWSUP = BER_BVC("newsuperior");
97 #define BV_CASEMATCH(a, b) \
98 ((a)->bv_len == (b)->bv_len && 0 == strcasecmp((a)->bv_val, (b)->bv_val))
100 static int process_ldif_rec LDAP_P(( char *rbuf, int lineno ));
101 static int parse_ldif_control LDAP_P(( struct berval *val, LDAPControl ***pctrls ));
102 static int domodify LDAP_P((
105 LDAPControl **pctrls,
107 static int dodelete LDAP_P((
109 LDAPControl **pctrls ));
110 static int dorename LDAP_P((
115 LDAPControl **pctrls ));
116 static int process_response(
124 static int txnabort = 0;
125 struct berval *txn_id = NULL;
131 fprintf( stderr, _("Add or modify entries from an LDAP server\n\n"));
132 fprintf( stderr, _("usage: %s [options]\n"), prog);
133 fprintf( stderr, _(" The list of desired operations are read from stdin"
134 " or from the file\n"));
135 fprintf( stderr, _(" specified by \"-f file\".\n"));
136 fprintf( stderr, _("Add or modify options:\n"));
137 fprintf( stderr, _(" -a add values (%s)\n"),
138 (ldapadd ? _("default") : _("default is to replace")));
139 fprintf( stderr, _(" -c continuous operation mode (do not stop on errors)\n"));
140 fprintf( stderr, _(" -E [!]ext=extparam modify extensions"
141 " (! indicate s criticality)\n"));
142 fprintf( stderr, _(" -f file read operations from `file'\n"));
143 fprintf( stderr, _(" -M enable Manage DSA IT control (-MM to make critical)\n"));
144 fprintf( stderr, _(" -P version protocol version (default: 3)\n"));
147 _(" [!]txn=<commit|abort> (transaction)\n"));
149 fprintf( stderr, _(" -S file write skipped modifications to `file'\n"));
152 exit( EXIT_FAILURE );
156 const char options[] = "aE:rS:"
157 "cd:D:e:f:h:H:IMnNO:o:p:P:QR:U:vVw:WxX:y:Y:Z";
160 handle_private_option( int i )
162 char *control, *cvalue;
166 case 'E': /* modify extensions */
167 if( protocol == LDAP_VERSION2 ) {
168 fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
170 exit( EXIT_FAILURE );
173 /* should be extended to support comma separated list of
174 * [!]key[=value] parameters, e.g. -E !foo,bar=567
179 if( optarg[0] == '!' ) {
184 control = ber_strdup( optarg );
185 if ( (cvalue = strchr( control, '=' )) != NULL ) {
190 if( strcasecmp( control, "txn" ) == 0 ) {
194 _("txn control previously specified\n"));
195 exit( EXIT_FAILURE );
197 if( cvalue != NULL ) {
198 if( strcasecmp( cvalue, "abort" ) == 0 ) {
200 } else if( strcasecmp( cvalue, "commit" ) != 0 ) {
201 fprintf( stderr, _("Invalid value for txn control, %s\n"),
203 exit( EXIT_FAILURE );
211 fprintf( stderr, _("Invalid modify extension name: %s\n"),
221 case 'r': /* replace (obsolete) */
224 case 'S': /* skipped modifications to file */
225 if( rejfile != NULL ) {
226 fprintf( stderr, _("%s: -S previously specified\n"), prog );
227 exit( EXIT_FAILURE );
229 rejfile = ber_strdup( optarg );
240 main( int argc, char **argv )
242 char *rbuf = NULL, *rejbuf = NULL;
244 struct LDIFFP *ldiffp, ldifdummy = {0};
245 char *matched_msg, *error_msg;
246 int rc, retval, ldifrc;
249 int lineno, nextline = 0, lmax = 0;
252 prog = lutil_progname( "ldapmodify", argc, argv );
254 /* strncmp instead of strcmp since NT binaries carry .exe extension */
255 ldapadd = ( strncasecmp( prog, "ldapadd", sizeof("ldapadd")-1 ) == 0 );
257 tool_init( ldapadd ? TOOL_ADD : TOOL_MODIFY );
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 (( ldiffp = ldif_open( infile, "r" )) == NULL ) {
275 return( EXIT_FAILURE );
278 ldifdummy.fp = stdin;
282 if ( debug ) ldif_debug = debug;
284 ld = tool_conn_setup( dont, 0 );
292 /* start transaction */
293 rc = ldap_txn_start_s( ld, NULL, NULL, &txn_id );
294 if( rc != LDAP_SUCCESS ) {
295 tool_perror( "ldap_txn_start_s", rc, NULL, NULL, NULL, NULL );
296 if( txn > 1 ) return EXIT_FAILURE;
310 c[i].ldctl_oid = LDAP_CONTROL_X_TXN_SPEC;
311 c[i].ldctl_value = *txn_id;
312 c[i].ldctl_iscritical = 1;
318 tool_server_controls( ld, c, i );
323 while (( rc == 0 || contoper ) && ( ldifrc = ldif_read_record( ldiffp, &nextline,
324 &rbuf, &lmax )) > 0 )
327 len = strlen( rbuf );
328 if (( rejbuf = (char *)ber_memalloc( len+1 )) == NULL ) {
330 exit( EXIT_FAILURE );
332 memcpy( rejbuf, rbuf, len+1 );
335 rc = process_ldif_rec( rbuf, lineno );
338 if ( rc ) retval = rc;
340 fprintf(rejfp, _("# Error: %s (%d)"), ldap_err2string(rc), rc);
343 ldap_get_option(ld, LDAP_OPT_MATCHED_DN, &matched_msg);
344 if ( matched_msg != NULL ) {
345 if ( *matched_msg != '\0' ) {
346 fprintf( rejfp, _(", matched DN: %s"), matched_msg );
348 ldap_memfree( matched_msg );
352 ldap_get_option(ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, &error_msg);
353 if ( error_msg != NULL ) {
354 if ( *error_msg != '\0' ) {
355 fprintf( rejfp, _(", additional info: %s"), error_msg );
357 ldap_memfree( error_msg );
359 fprintf( rejfp, "\n%s\n", rejbuf );
362 if (rejfp) ber_memfree( rejbuf );
370 if( retval == 0 && txn ) {
371 rc = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
372 if ( rc != LDAP_OPT_SUCCESS ) {
373 fprintf( stderr, "Could not unset controls for ldap_txn_end\n");
376 /* create transaction */
377 rc = ldap_txn_end_s( ld, !txnabort, txn_id, NULL, NULL, NULL );
378 if( rc != LDAP_SUCCESS ) {
379 tool_perror( "ldap_txn_end_s", rc, NULL, NULL, NULL, NULL );
389 if ( rejfp != NULL ) {
399 process_ldif_rec( char *rbuf, int linenum )
401 char *line, *dn, *newrdn, *newsup;
403 int expect_modop, expect_sep;
405 int new_entry, delete_entry, got_all;
406 LDAPMod **pmods, *lm = NULL;
408 LDAPControl **pctrls;
409 int i, j, k, lines, idn, nmods;
410 struct berval *btype, *vals, **bvl, bv;
412 unsigned char *mops = NULL;
416 rc = got_all = delete_entry = modop = expect_modop = 0;
422 dn = newrdn = newsup = NULL;
424 lines = ldif_countlines( rbuf );
425 btype = ber_memcalloc( 1, (lines+1)*2*sizeof(struct berval)+lines );
427 return LDAP_NO_MEMORY;
429 vals = btype+lines+1;
430 freeval = (char *)(vals+lines+1);
433 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
436 if ( *line == '\n' || *line == '\0' ) {
442 if ( line[0] == '-' && !line[1] ) {
443 BER_BVZERO( btype+i );
448 if ( ( rc = ldif_parse_line2( line, btype+i, vals+i, &freev ) ) < 0 ) {
449 fprintf( stderr, _("%s: invalid format (line %d) entry: \"%s\"\n"),
450 prog, linenum+i, dn == NULL ? "" : dn );
451 rc = LDAP_PARAM_ERROR;
457 if ( linenum+i == 1 && BV_CASEMATCH( btype+i, &BV_VERSION )) {
459 if( vals[i].bv_len == 0 || lutil_atoi( &v, vals[i].bv_val) != 0 || v != 1 ) {
461 _("%s: invalid version %s, line %d (ignored)\n"),
462 prog, vals[i].bv_val, linenum );
466 } else if ( BV_CASEMATCH( btype+i, &BV_DN )) {
470 /* skip all lines until we see "dn:" */
474 /* check to make sure there was a dn: line */
487 if( version && lines == 1 ) {
493 /* Check for "control" tag after dn and before changetype. */
494 if ( BV_CASEMATCH( btype+i, &BV_CONTROL )) {
495 /* Parse and add it to the list of controls */
496 rc = parse_ldif_control( vals+i, &pctrls );
499 _("%s: Error processing %s line, line %d: %s\n"),
500 prog, BV_CONTROL.bv_val, linenum+i, ldap_err2string(rc) );
506 _("%s: Expecting more input after %s line, line %d\n"),
507 prog, btype[i-1].bv_val, linenum+i );
509 rc = LDAP_PARAM_ERROR;
514 /* Check for changetype */
515 if ( BV_CASEMATCH( btype+i, &BV_CHANGETYPE )) {
516 #ifdef LIBERAL_CHANGETYPE_MODOP
517 /* trim trailing spaces (and log warning ...) */
519 for ( icnt = vals[i].bv_len; --icnt > 0; ) {
520 if ( !isspace( (unsigned char) vals[i].bv_val[icnt] ) ) {
525 if ( ++icnt != vals[i].bv_len ) {
526 fprintf( stderr, _("%s: illegal trailing space after"
527 " \"%s: %s\" trimmed (line %d, entry \"%s\")\n"),
528 prog, BV_CHANGETYPE.bv_val, vals[i].bv_val, linenum+i, dn );
529 vals[i].bv_val[icnt] = '\0';
531 #endif /* LIBERAL_CHANGETYPE_MODOP */
533 if ( BV_CASEMATCH( vals+i, &BV_MODIFYCT )) {
536 } else if ( BV_CASEMATCH( vals+i, &BV_ADDCT )) {
538 modop = LDAP_MOD_ADD;
539 } else if ( BV_CASEMATCH( vals+i, &BV_MODRDNCT )
540 || BV_CASEMATCH( vals+i, &BV_MODDNCT )
541 || BV_CASEMATCH( vals+i, &BV_RENAMECT ))
546 if ( !BV_CASEMATCH( btype+i, &BV_NEWRDN )) {
547 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
548 " \"%s:\" (line %d, entry \"%s\")\n"),
549 prog, BV_NEWRDN.bv_val, btype[i].bv_val, linenum+i, dn );
550 rc = LDAP_PARAM_ERROR;
553 newrdn = vals[i].bv_val;
557 if ( !BV_CASEMATCH( btype+i, &BV_DELETEOLDRDN )) {
558 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
559 " \"%s:\" (line %d, entry \"%s\")\n"),
560 prog, BV_DELETEOLDRDN.bv_val, btype[i].bv_val, linenum+i, dn );
561 rc = LDAP_PARAM_ERROR;
564 deleteoldrdn = ( vals[i].bv_val[0] == '0' ) ? 0 : 1;
567 if ( !BV_CASEMATCH( btype+i, &BV_NEWSUP )) {
568 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
569 " \"%s:\" (line %d, entry \"%s\")\n"),
570 prog, BV_NEWSUP.bv_val, btype[i].bv_val, linenum+i, dn );
571 rc = LDAP_PARAM_ERROR;
574 newsup = vals[i].bv_val;
578 } else if ( BV_CASEMATCH( vals+i, &BV_DELETECT )) {
579 got_all = delete_entry = 1;
582 _("%s: unknown %s \"%s\" (line %d, entry \"%s\")\n"),
583 prog, BV_CHANGETYPE.bv_val, vals[i].bv_val, linenum+i, dn );
584 rc = LDAP_PARAM_ERROR;
588 } else if ( ldapadd ) { /* missing changetype => add */
590 modop = LDAP_MOD_ADD;
592 expect_modop = 1; /* missing changetype => modify */
598 _("%s: extra lines at end (line %d, entry \"%s\")\n"),
599 prog, linenum+i, dn );
600 rc = LDAP_PARAM_ERROR;
612 /* Make sure all attributes with multiple values are contiguous */
613 for (; i<lines; i++) {
614 for (j=i+1; j<lines; j++) {
615 if ( BV_CASEMATCH( btype+i, btype+j )) {
617 /* out of order, move intervening attributes down */
621 for (k=j; k>i; k--) {
622 btype[k] = btype[k-1];
624 freeval[k] = freeval[k-1];
635 /* Allocate space for array of mods, array of pointers to mods,
636 * and array of pointers to values, allowing for NULL terminators
637 * for the pointer arrays...
639 lm = ber_memalloc( nmods * sizeof(LDAPMod) +
640 (nmods+1) * sizeof(LDAPMod*) +
641 (lines + nmods - idn) * sizeof(struct berval *));
642 pmods = (LDAPMod **)(lm+nmods);
643 bvl = (struct berval **)(pmods+nmods+1);
648 for (i=idn; i<lines; i++) {
649 if ( BV_CASEMATCH( btype+i, &BV_DN )) {
650 fprintf( stderr, _("%s: attributeDescription \"%s\":"
651 " (possible missing newline"
652 " after line %d, entry \"%s\"?)\n"),
653 prog, btype[i].bv_val, linenum+i - 1, dn );
655 if ( !BV_CASEMATCH( btype+i, &bv )) {
658 lm[j].mod_op = LDAP_MOD_ADD | LDAP_MOD_BVALUES;
659 lm[j].mod_type = bv.bv_val;
660 lm[j].mod_bvalues = bvl+k;
671 mops = ber_memalloc( lines+1 );
675 for ( ; i<lines; i++ ) {
676 if ( expect_modop ) {
677 #ifdef LIBERAL_CHANGETYPE_MODOP
678 /* trim trailing spaces (and log warning ...) */
680 for ( icnt = vals[i].bv_len; --icnt > 0; ) {
681 if ( !isspace( (unsigned char) vals[i].bv_val[icnt] ) ) break;
684 if ( ++icnt != vals[i].bv_len ) {
685 fprintf( stderr, _("%s: illegal trailing space after"
686 " \"%s: %s\" trimmed (line %d, entry \"%s\")\n"),
687 prog, type, vals[i].bv_val, linenum+i, dn );
688 vals[i].bv_val[icnt] = '\0';
690 #endif /* LIBERAL_CHANGETYPE_MODOP */
694 if ( BV_CASEMATCH( btype+i, &BV_MODOPADD )) {
695 modop = LDAP_MOD_ADD;
698 } else if ( BV_CASEMATCH( btype+i, &BV_MODOPREPLACE )) {
699 /* defer handling these since they might have no values.
700 * Use the BVALUES flag to signal that these were
701 * deferred. If values are provided later, this
702 * flag will be switched off.
704 modop = LDAP_MOD_REPLACE;
705 mops[i] = modop | LDAP_MOD_BVALUES;
707 } else if ( BV_CASEMATCH( btype+i, &BV_MODOPDELETE )) {
708 modop = LDAP_MOD_DELETE;
709 mops[i] = modop | LDAP_MOD_BVALUES;
711 } else if ( BV_CASEMATCH( btype+i, &BV_MODOPINCREMENT )) {
712 modop = LDAP_MOD_INCREMENT;
715 } else { /* no modify op: invalid LDIF */
716 fprintf( stderr, _("%s: modify operation type is missing at"
717 " line %d, entry \"%s\"\n"),
718 prog, linenum+i, dn );
719 rc = LDAP_PARAM_ERROR;
723 } else if ( expect_sep && BER_BVISEMPTY( btype+i )) {
729 if ( !BV_CASEMATCH( btype+i, &bv )) {
730 fprintf( stderr, _("%s: wrong attributeType at"
731 " line %d, entry \"%s\"\n"),
732 prog, linenum+i, dn );
733 rc = LDAP_PARAM_ERROR;
737 /* If prev op was deferred and matches this type,
740 if ( (mops[i-1] & LDAP_MOD_BVALUES)
741 && BV_CASEMATCH( btype+i, btype+i-1 ))
749 #if 0 /* we should faithfully encode the LDIF, not combine */
750 /* Make sure all modops with multiple values are contiguous */
751 for (i=idn; i<lines; i++) {
752 if ( mops[i] == M_SEP )
754 for (j=i+1; j<lines; j++) {
755 if ( mops[j] == M_SEP || mops[i] != mops[j] )
757 if ( BV_CASEMATCH( btype+i, btype+j )) {
759 /* out of order, move intervening attributes down */
768 for (k=j; k>i; k--) {
769 btype[k] = btype[k-1];
771 freeval[k] = freeval[k-1];
786 /* Allocate space for array of mods, array of pointers to mods,
787 * and array of pointers to values, allowing for NULL terminators
788 * for the pointer arrays...
790 lm = ber_memalloc( nmods * sizeof(LDAPMod) +
791 (nmods+1) * sizeof(LDAPMod*) +
792 (lines + nmods - idn) * sizeof(struct berval *));
793 pmods = (LDAPMod **)(lm+nmods);
794 bvl = (struct berval **)(pmods+nmods+1);
800 for (i=idn; i<lines; i++) {
801 if ( mops[i] == M_SEP )
803 if ( mops[i] != mops[i-1] || !BV_CASEMATCH( btype+i, &bv )) {
806 lm[j].mod_op = mops[i] | LDAP_MOD_BVALUES;
807 lm[j].mod_type = bv.bv_val;
808 if ( mops[i] & LDAP_MOD_BVALUES ) {
809 lm[j].mod_bvalues = NULL;
811 lm[j].mod_bvalues = bvl+k;
822 /* If default controls are set (as with -M option) and controls are
823 specified in the LDIF file, we must add the default controls to
824 the list of controls sent with the ldap operation.
828 LDAPControl **defctrls = NULL; /* Default server controls */
829 LDAPControl **newctrls = NULL;
830 ldap_get_option(ld, LDAP_OPT_SERVER_CONTROLS, &defctrls);
832 int npc=0; /* Num of LDIF controls */
833 int ndefc=0; /* Num of default controls */
834 while (pctrls[npc]) npc++; /* Count LDIF controls */
835 while (defctrls[ndefc]) ndefc++; /* Count default controls */
836 newctrls = ber_memrealloc(pctrls,
837 (npc+ndefc+1)*sizeof(LDAPControl*));
839 if (newctrls == NULL) {
844 for (i=npc; i<npc+ndefc; i++) {
845 pctrls[i] = ldap_control_dup(defctrls[i-npc]);
846 if (pctrls[i] == NULL) {
851 pctrls[npc+ndefc] = NULL;
853 ldap_controls_free(defctrls); /* Must be freed by library */
859 if ( delete_entry ) {
860 rc = dodelete( dn, pctrls );
861 } else if ( newrdn != NULL ) {
862 rc = dorename( dn, newrdn, newsup, deleteoldrdn, pctrls );
864 rc = domodify( dn, pmods, pctrls, new_entry );
867 if ( rc == LDAP_SUCCESS ) {
873 if (pctrls != NULL) {
874 ldap_controls_free( pctrls );
879 if ( mops != NULL ) {
882 for (i=lines-1; i>=0; i--)
883 if ( freeval[i] ) ber_memfree( vals[i].bv_val );
884 ber_memfree( btype );
889 /* Parse an LDIF control line of the form
890 control: oid [true/false] [: value] or
891 control: oid [true/false] [:: base64-value] or
892 control: oid [true/false] [:< url]
893 The control is added to the list of controls in *ppctrls.
898 LDAPControl ***ppctrls )
901 int criticality = 0; /* Default is false if not present */
904 LDAPControl *newctrl = NULL;
905 LDAPControl **pctrls = NULL;
906 struct berval type, bv;
909 if (ppctrls) pctrls = *ppctrls;
910 /* OID should come first. Validate and extract it. */
912 if (*s == 0) return ( LDAP_PARAM_ERROR );
914 while (isdigit((unsigned char)*s) || *s == '.') {
915 s++; /* OID should be digits or . */
918 return ( LDAP_PARAM_ERROR ); /* OID was not present */
920 if (*s) { /* End of OID should be space or NULL */
921 if (!isspace((unsigned char)*s)) {
922 return ( LDAP_PARAM_ERROR ); /* else OID contained invalid chars */
924 *s++ = 0; /* Replace space with null to terminate */
927 oid = ber_strdup(oidStart);
928 if (oid == NULL) return ( LDAP_NO_MEMORY );
930 /* Optional Criticality field is next. */
931 while (*s && isspace((unsigned char)*s)) {
932 s++; /* Skip white space before criticality */
934 if (strncasecmp(s, "true", 4) == 0) {
938 else if (strncasecmp(s, "false", 5) == 0) {
943 /* Optional value field is next */
944 while (*s && isspace((unsigned char)*s)) {
945 s++; /* Skip white space before value */
948 if (*s != ':') { /* If value is present, must start with : */
949 rc = LDAP_PARAM_ERROR;
953 /* Back up so value is in the form
957 Then we can use ldif_parse_line2 to extract and decode the value
962 rc = ldif_parse_line2(s, &type, &bv, &freeval);
964 rc = LDAP_PARAM_ERROR;
969 /* Create a new LDAPControl structure. */
970 newctrl = (LDAPControl *)ber_memalloc(sizeof(LDAPControl));
971 if ( newctrl == NULL ) {
975 newctrl->ldctl_oid = oid;
977 newctrl->ldctl_iscritical = criticality;
979 newctrl->ldctl_value = bv;
981 ber_dupbv( &newctrl->ldctl_value, &bv );
983 /* Add the new control to the passed-in list of controls. */
986 while ( pctrls[i] ) { /* Count the # of controls passed in */
990 /* Allocate 1 more slot for the new control and 1 for the NULL. */
991 pctrls = (LDAPControl **) ber_memrealloc(pctrls,
992 (i+2)*(sizeof(LDAPControl *)));
993 if (pctrls == NULL) {
1004 if (newctrl->ldctl_oid) ber_memfree(newctrl->ldctl_oid);
1005 if (newctrl->ldctl_value.bv_val) {
1006 ber_memfree(newctrl->ldctl_value.bv_val);
1008 ber_memfree(newctrl);
1010 if (oid) ber_memfree(oid);
1020 LDAPControl **pctrls,
1023 int rc, i, j, k, notascii, op;
1027 fprintf( stderr, _("%s: no DN specified\n"), prog );
1028 return( LDAP_PARAM_ERROR );
1031 if ( pmods == NULL ) {
1032 /* implement "touch" (empty sequence)
1033 * modify operation (note that there
1034 * is no symmetry with the UNIX command,
1035 * since \"touch\" on a non-existent entry
1037 printf( "warning: no attributes to %sadd (entry=\"%s\")\n",
1038 newentry ? "" : "change or ", dn );
1041 for ( i = 0; pmods[ i ] != NULL; ++i ) {
1042 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1043 if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
1045 _("%s: attribute \"%s\" has no values (entry=\"%s\")\n"),
1046 prog, pmods[i]->mod_type, dn );
1047 return LDAP_PARAM_ERROR;
1052 for ( i = 0; pmods[ i ] != NULL; ++i ) {
1053 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1055 op == LDAP_MOD_REPLACE ? _("replace") :
1056 op == LDAP_MOD_ADD ? _("add") :
1057 op == LDAP_MOD_INCREMENT ? _("increment") :
1058 op == LDAP_MOD_DELETE ? _("delete") :
1060 pmods[ i ]->mod_type );
1062 if ( pmods[ i ]->mod_bvalues != NULL ) {
1063 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1064 bvp = pmods[ i ]->mod_bvalues[ j ];
1066 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
1067 if ( !isascii( bvp->bv_val[ k ] )) {
1073 printf( _("\tNOT ASCII (%ld bytes)\n"), bvp->bv_len );
1075 printf( "\t%s\n", bvp->bv_val );
1084 printf( "%sadding new entry \"%s\"\n", dont ? "!" : "", dn );
1086 printf( "%smodifying entry \"%s\"\n", dont ? "!" : "", dn );
1092 rc = ldap_add_ext( ld, dn, pmods, pctrls, NULL, &msgid );
1094 rc = ldap_modify_ext( ld, dn, pmods, pctrls, NULL, &msgid );
1097 if ( rc != LDAP_SUCCESS ) {
1098 /* print error message about failed update including DN */
1099 fprintf( stderr, _("%s: update failed: %s\n"), prog, dn );
1100 tool_perror( newentry ? "ldap_add" : "ldap_modify",
1101 rc, NULL, NULL, NULL, NULL );
1104 rc = process_response( ld, msgid,
1105 newentry ? LDAP_RES_ADD : LDAP_RES_MODIFY, dn );
1107 if ( verbose && rc == LDAP_SUCCESS ) {
1108 printf( _("modify complete\n") );
1124 LDAPControl **pctrls )
1129 printf( _("%sdeleting entry \"%s\"\n"), dont ? "!" : "", dn );
1131 rc = ldap_delete_ext( ld, dn, pctrls, NULL, &msgid );
1132 if ( rc != LDAP_SUCCESS ) {
1133 fprintf( stderr, _("%s: delete failed: %s\n"), prog, dn );
1134 tool_perror( "ldap_delete", rc, NULL, NULL, NULL, NULL );
1137 rc = process_response( ld, msgid, LDAP_RES_DELETE, dn );
1139 if ( verbose && rc == LDAP_SUCCESS ) {
1140 printf( _("delete complete\n") );
1158 LDAPControl **pctrls )
1163 printf( _("%smodifying rdn of entry \"%s\"\n"), dont ? "!" : "", dn );
1165 printf( _("\tnew RDN: \"%s\" (%skeep existing values)\n"),
1166 newrdn, deleteoldrdn ? _("do not ") : "" );
1169 rc = ldap_rename( ld, dn, newrdn, newsup, deleteoldrdn,
1170 pctrls, NULL, &msgid );
1171 if ( rc != LDAP_SUCCESS ) {
1172 fprintf( stderr, _("%s: rename failed: %s\n"), prog, dn );
1173 tool_perror( "ldap_rename", rc, NULL, NULL, NULL, NULL );
1176 rc = process_response( ld, msgid, LDAP_RES_RENAME, dn );
1178 if ( verbose && rc == LDAP_SUCCESS ) {
1179 printf( _("rename complete\n") );
1191 res2str( int res ) {
1195 case LDAP_RES_DELETE:
1196 return "ldap_delete";
1197 case LDAP_RES_MODIFY:
1198 return "ldap_modify";
1199 case LDAP_RES_MODRDN:
1200 return "ldap_rename";
1205 return "ldap_unknown";
1208 static int process_response(
1215 int rc = LDAP_OTHER, msgtype;
1216 struct timeval tv = { 0, 0 };
1218 char *text = NULL, *matched = NULL, **refs = NULL;
1219 LDAPControl **ctrls = NULL;
1223 tv.tv_usec = 100000;
1225 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
1226 if ( tool_check_abandon( ld, msgid ) ) {
1227 return LDAP_CANCELLED;
1231 ldap_get_option( ld, LDAP_OPT_RESULT_CODE, &rc );
1232 tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
1241 msgtype = ldap_msgtype( res );
1243 rc = ldap_parse_result( ld, res, &err, &matched, &text, &refs, &ctrls, 1 );
1244 if ( rc == LDAP_SUCCESS ) rc = err;
1247 if ( rc == LDAP_X_TXN_SPECIFY_OKAY ) {
1251 if ( rc != LDAP_SUCCESS ) {
1252 tool_perror( res2str( op ), rc, NULL, matched, text, refs );
1253 } else if ( msgtype != op ) {
1254 fprintf( stderr, "%s: msgtype: expected %d got %d\n",
1255 res2str( op ), op, msgtype );
1259 if ( text ) ldap_memfree( text );
1260 if ( matched ) ldap_memfree( matched );
1261 if ( refs ) ber_memvfree( (void **)refs );
1264 tool_print_ctrls( ld, ctrls );
1265 ldap_controls_free( ctrls );