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-2009 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"
74 static char *rejfile = NULL;
75 static LDAP *ld = NULL;
79 /* strings found in LDIF entries */
80 static struct berval BV_VERSION = BER_BVC("version");
81 static struct berval BV_DN = BER_BVC("dn");
82 static struct berval BV_CONTROL = BER_BVC("control");
83 static struct berval BV_CHANGETYPE = BER_BVC("changetype");
84 static struct berval BV_ADDCT = BER_BVC("add");
85 static struct berval BV_MODIFYCT = BER_BVC("modify");
86 static struct berval BV_DELETECT = BER_BVC("delete");
87 static struct berval BV_MODRDNCT = BER_BVC("modrdn");
88 static struct berval BV_MODDNCT = BER_BVC("moddn");
89 static struct berval BV_RENAMECT = BER_BVC("rename");
90 static struct berval BV_MODOPADD = BER_BVC("add");
91 static struct berval BV_MODOPREPLACE = BER_BVC("replace");
92 static struct berval BV_MODOPDELETE = BER_BVC("delete");
93 static struct berval BV_MODOPINCREMENT = BER_BVC("increment");
94 static struct berval BV_NEWRDN = BER_BVC("newrdn");
95 static struct berval BV_DELETEOLDRDN = BER_BVC("deleteoldrdn");
96 static struct berval BV_NEWSUP = BER_BVC("newsuperior");
98 #define BV_CASEMATCH(a, b) \
99 ((a)->bv_len == (b)->bv_len && 0 == strcasecmp((a)->bv_val, (b)->bv_val))
101 static int process_ldif_rec LDAP_P(( char *rbuf, int lineno ));
102 static int parse_ldif_control LDAP_P(( struct berval *val, LDAPControl ***pctrls ));
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(
125 static int txnabort = 0;
126 struct berval *txn_id = NULL;
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"));
144 _(" [!]txn=<commit|abort> (transaction)\n"));
146 fprintf( stderr, _(" -S file write skipped modifications to `file'\n"));
149 exit( EXIT_FAILURE );
153 const char options[] = "aE:rS:"
154 "cd:D:e:f:h:H:IMnNO:o:p:P:QR:U:vVw:WxX:y:Y:Z";
157 handle_private_option( int i )
159 char *control, *cvalue;
163 case 'E': /* modify extensions */
164 if( protocol == LDAP_VERSION2 ) {
165 fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
167 exit( EXIT_FAILURE );
170 /* should be extended to support comma separated list of
171 * [!]key[=value] parameters, e.g. -E !foo,bar=567
176 if( optarg[0] == '!' ) {
181 control = ber_strdup( optarg );
182 if ( (cvalue = strchr( control, '=' )) != NULL ) {
187 if( strcasecmp( control, "txn" ) == 0 ) {
191 _("txn control previously specified\n"));
192 exit( EXIT_FAILURE );
194 if( cvalue != NULL ) {
195 if( strcasecmp( cvalue, "abort" ) == 0 ) {
197 } else if( strcasecmp( cvalue, "commit" ) != 0 ) {
198 fprintf( stderr, _("Invalid value for txn control, %s\n"),
200 exit( EXIT_FAILURE );
208 fprintf( stderr, _("Invalid modify extension name: %s\n"),
218 case 'r': /* replace (obsolete) */
221 case 'S': /* skipped modifications to file */
222 if( rejfile != NULL ) {
223 fprintf( stderr, _("%s: -S previously specified\n"), prog );
224 exit( EXIT_FAILURE );
226 rejfile = ber_strdup( optarg );
237 main( int argc, char **argv )
239 char *rbuf = NULL, *rejbuf = NULL;
241 struct LDIFFP *ldiffp, ldifdummy = {0};
242 char *matched_msg, *error_msg;
243 int rc, retval, ldifrc;
246 int lineno, nextline = 0, lmax = 0;
249 prog = lutil_progname( "ldapmodify", argc, argv );
251 /* strncmp instead of strcmp since NT binaries carry .exe extension */
252 ldapadd = ( strncasecmp( prog, "ldapadd", sizeof("ldapadd")-1 ) == 0 );
254 tool_init( ldapadd ? TOOL_ADD : TOOL_MODIFY );
256 tool_args( argc, argv );
258 if ( argc != optind ) usage();
260 if ( rejfile != NULL ) {
261 if (( rejfp = fopen( rejfile, "w" )) == NULL ) {
263 return( EXIT_FAILURE );
269 if ( infile != NULL ) {
270 if (( ldiffp = ldif_open( infile, "r" )) == NULL ) {
272 return( EXIT_FAILURE );
275 ldifdummy.fp = stdin;
279 if ( debug ) ldif_debug = debug;
281 ld = tool_conn_setup( dont, 0 );
284 if ( pw_file || want_bindpw ) {
286 rc = lutil_get_filed_password( pw_file, &passwd );
287 if( rc ) return EXIT_FAILURE;
289 passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
290 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
298 /* start transaction */
299 rc = ldap_txn_start_s( ld, NULL, NULL, &txn_id );
300 if( rc != LDAP_SUCCESS ) {
301 tool_perror( "ldap_txn_start_s", rc, NULL, NULL, NULL, NULL );
302 if( txn > 1 ) return EXIT_FAILURE;
316 c[i].ldctl_oid = LDAP_CONTROL_X_TXN_SPEC;
317 c[i].ldctl_value = *txn_id;
318 c[i].ldctl_iscritical = 1;
324 tool_server_controls( ld, c, i );
329 while (( rc == 0 || contoper ) && ( ldifrc = ldif_read_record( ldiffp, &nextline,
330 &rbuf, &lmax )) > 0 )
333 len = strlen( rbuf );
334 if (( rejbuf = (char *)ber_memalloc( len+1 )) == NULL ) {
336 exit( EXIT_FAILURE );
338 memcpy( rejbuf, rbuf, len+1 );
341 rc = process_ldif_rec( rbuf, lineno );
344 if ( rc ) retval = rc;
346 fprintf(rejfp, _("# Error: %s (%d)"), ldap_err2string(rc), rc);
349 ldap_get_option(ld, LDAP_OPT_MATCHED_DN, &matched_msg);
350 if ( matched_msg != NULL ) {
351 if ( *matched_msg != '\0' ) {
352 fprintf( rejfp, _(", matched DN: %s"), matched_msg );
354 ldap_memfree( matched_msg );
358 ldap_get_option(ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, &error_msg);
359 if ( error_msg != NULL ) {
360 if ( *error_msg != '\0' ) {
361 fprintf( rejfp, _(", additional info: %s"), error_msg );
363 ldap_memfree( error_msg );
365 fprintf( rejfp, "\n%s\n", rejbuf );
368 if (rejfp) ber_memfree( rejbuf );
376 if( retval == 0 && txn ) {
377 rc = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
378 if ( rc != LDAP_OPT_SUCCESS ) {
379 fprintf( stderr, "Could not unset controls for ldap_txn_end\n");
382 /* create transaction */
383 rc = ldap_txn_end_s( ld, !txnabort, txn_id, NULL, NULL, NULL );
384 if( rc != LDAP_SUCCESS ) {
385 tool_perror( "ldap_txn_end_s", rc, NULL, NULL, NULL, NULL );
395 if ( rejfp != NULL ) {
405 process_ldif_rec( char *rbuf, int linenum )
407 char *line, *dn, *newrdn, *newsup;
409 int expect_modop, expect_sep;
411 int new_entry, delete_entry, got_all;
412 LDAPMod **pmods, *lm = NULL;
414 LDAPControl **pctrls;
415 int i, j, k, lines, idn, nmods;
416 struct berval *btype, *vals, **bvl, bv;
418 unsigned char *mops = NULL;
422 rc = got_all = delete_entry = modop = expect_modop = 0;
428 dn = newrdn = newsup = NULL;
430 lines = ldif_countlines( rbuf );
431 btype = ber_memcalloc( 1, (lines+1)*2*sizeof(struct berval)+lines );
433 return LDAP_NO_MEMORY;
435 vals = btype+lines+1;
436 freeval = (char *)(vals+lines+1);
439 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
442 if ( *line == '\n' || *line == '\0' ) {
448 if ( line[0] == '-' && !line[1] ) {
449 BER_BVZERO( btype+i );
454 if ( ( rc = ldif_parse_line2( line, btype+i, vals+i, &freev ) ) < 0 ) {
455 fprintf( stderr, _("%s: invalid format (line %d) entry: \"%s\"\n"),
456 prog, linenum+i, dn == NULL ? "" : dn );
457 rc = LDAP_PARAM_ERROR;
463 if ( linenum+i == 1 && BV_CASEMATCH( btype+i, &BV_VERSION )) {
465 if( vals[i].bv_len == 0 || lutil_atoi( &v, vals[i].bv_val) != 0 || v != 1 ) {
467 _("%s: invalid version %s, line %d (ignored)\n"),
468 prog, vals[i].bv_val, linenum );
472 } else if ( BV_CASEMATCH( btype+i, &BV_DN )) {
476 /* skip all lines until we see "dn:" */
480 /* check to make sure there was a dn: line */
493 if( version && lines == 1 ) {
499 /* Check for "control" tag after dn and before changetype. */
500 if ( BV_CASEMATCH( btype+i, &BV_CONTROL )) {
501 /* Parse and add it to the list of controls */
502 rc = parse_ldif_control( vals+i, &pctrls );
505 _("%s: Error processing %s line, line %d: %s\n"),
506 prog, BV_CONTROL.bv_val, linenum+i, ldap_err2string(rc) );
512 _("%s: Expecting more input after %s line, line %d\n"),
513 prog, btype[i-1].bv_val, linenum+i );
515 rc = LDAP_PARAM_ERROR;
520 /* Check for changetype */
521 if ( BV_CASEMATCH( btype+i, &BV_CHANGETYPE )) {
522 #ifdef LIBERAL_CHANGETYPE_MODOP
523 /* trim trailing spaces (and log warning ...) */
525 for ( icnt = vals[i].bv_len; --icnt > 0; ) {
526 if ( !isspace( (unsigned char) vals[i].bv_val[icnt] ) ) {
531 if ( ++icnt != vals[i].bv_len ) {
532 fprintf( stderr, _("%s: illegal trailing space after"
533 " \"%s: %s\" trimmed (line %d, entry \"%s\")\n"),
534 prog, BV_CHANGETYPE.bv_val, vals[i].bv_val, linenum+i, dn );
535 vals[i].bv_val[icnt] = '\0';
537 #endif /* LIBERAL_CHANGETYPE_MODOP */
539 if ( BV_CASEMATCH( vals+i, &BV_MODIFYCT )) {
542 } else if ( BV_CASEMATCH( vals+i, &BV_ADDCT )) {
544 modop = LDAP_MOD_ADD;
545 } else if ( BV_CASEMATCH( vals+i, &BV_MODRDNCT )
546 || BV_CASEMATCH( vals+i, &BV_MODDNCT )
547 || BV_CASEMATCH( vals+i, &BV_RENAMECT ))
552 if ( !BV_CASEMATCH( btype+i, &BV_NEWRDN )) {
553 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
554 " \"%s:\" (line %d, entry \"%s\")\n"),
555 prog, BV_NEWRDN.bv_val, btype[i].bv_val, linenum+i, dn );
556 rc = LDAP_PARAM_ERROR;
559 newrdn = vals[i].bv_val;
563 if ( !BV_CASEMATCH( btype+i, &BV_DELETEOLDRDN )) {
564 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
565 " \"%s:\" (line %d, entry \"%s\")\n"),
566 prog, BV_DELETEOLDRDN.bv_val, btype[i].bv_val, linenum+i, dn );
567 rc = LDAP_PARAM_ERROR;
570 deleteoldrdn = ( vals[i].bv_val[0] == '0' ) ? 0 : 1;
573 if ( !BV_CASEMATCH( btype+i, &BV_NEWSUP )) {
574 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
575 " \"%s:\" (line %d, entry \"%s\")\n"),
576 prog, BV_NEWSUP.bv_val, btype[i].bv_val, linenum+i, dn );
577 rc = LDAP_PARAM_ERROR;
580 newsup = vals[i].bv_val;
584 } else if ( BV_CASEMATCH( vals+i, &BV_DELETECT )) {
585 got_all = delete_entry = 1;
588 _("%s: unknown %s \"%s\" (line %d, entry \"%s\")\n"),
589 prog, BV_CHANGETYPE.bv_val, vals[i].bv_val, linenum+i, dn );
590 rc = LDAP_PARAM_ERROR;
594 } else if ( ldapadd ) { /* missing changetype => add */
596 modop = LDAP_MOD_ADD;
598 expect_modop = 1; /* missing changetype => modify */
604 _("%s: extra lines at end (line %d, entry \"%s\")\n"),
605 prog, linenum+i, dn );
606 rc = LDAP_PARAM_ERROR;
618 /* Make sure all attributes with multiple values are contiguous */
619 for (; i<lines; i++) {
620 for (j=i+1; j<lines; j++) {
621 if ( BV_CASEMATCH( btype+i, btype+j )) {
623 /* out of order, move intervening attributes down */
627 for (k=j; k>i; k--) {
628 btype[k] = btype[k-1];
630 freeval[k] = freeval[k-1];
641 /* Allocate space for array of mods, array of pointers to mods,
642 * and array of pointers to values, allowing for NULL terminators
643 * for the pointer arrays...
645 lm = ber_memalloc( nmods * sizeof(LDAPMod) +
646 (nmods+1) * sizeof(LDAPMod*) +
647 (lines + nmods - idn) * sizeof(struct berval *));
648 pmods = (LDAPMod **)(lm+nmods);
649 bvl = (struct berval **)(pmods+nmods+1);
654 for (i=idn; i<lines; i++) {
655 if ( BV_CASEMATCH( btype+i, &BV_DN )) {
656 fprintf( stderr, _("%s: attributeDescription \"%s\":"
657 " (possible missing newline"
658 " after line %d, entry \"%s\"?)\n"),
659 prog, btype[i].bv_val, linenum+i - 1, dn );
661 if ( !BV_CASEMATCH( btype+i, &bv )) {
664 lm[j].mod_op = LDAP_MOD_ADD | LDAP_MOD_BVALUES;
665 lm[j].mod_type = bv.bv_val;
666 lm[j].mod_bvalues = bvl+k;
677 mops = ber_memalloc( lines+1 );
681 for ( ; i<lines; i++ ) {
682 if ( expect_modop ) {
683 #ifdef LIBERAL_CHANGETYPE_MODOP
684 /* trim trailing spaces (and log warning ...) */
686 for ( icnt = vals[i].bv_len; --icnt > 0; ) {
687 if ( !isspace( (unsigned char) vals[i].bv_val[icnt] ) ) break;
690 if ( ++icnt != vals[i].bv_len ) {
691 fprintf( stderr, _("%s: illegal trailing space after"
692 " \"%s: %s\" trimmed (line %d, entry \"%s\")\n"),
693 prog, type, vals[i].bv_val, linenum+i, dn );
694 vals[i].bv_val[icnt] = '\0';
696 #endif /* LIBERAL_CHANGETYPE_MODOP */
700 if ( BV_CASEMATCH( btype+i, &BV_MODOPADD )) {
701 modop = LDAP_MOD_ADD;
704 } else if ( BV_CASEMATCH( btype+i, &BV_MODOPREPLACE )) {
705 /* defer handling these since they might have no values.
706 * Use the BVALUES flag to signal that these were
707 * deferred. If values are provided later, this
708 * flag will be switched off.
710 modop = LDAP_MOD_REPLACE;
711 mops[i] = modop | LDAP_MOD_BVALUES;
713 } else if ( BV_CASEMATCH( btype+i, &BV_MODOPDELETE )) {
714 modop = LDAP_MOD_DELETE;
715 mops[i] = modop | LDAP_MOD_BVALUES;
717 } else if ( BV_CASEMATCH( btype+i, &BV_MODOPINCREMENT )) {
718 modop = LDAP_MOD_INCREMENT;
721 } else { /* no modify op: invalid LDIF */
722 fprintf( stderr, _("%s: modify operation type is missing at"
723 " line %d, entry \"%s\"\n"),
724 prog, linenum+i, dn );
725 rc = LDAP_PARAM_ERROR;
729 } else if ( expect_sep && BER_BVISEMPTY( btype+i )) {
735 if ( !BV_CASEMATCH( btype+i, &bv )) {
736 fprintf( stderr, _("%s: wrong attributeType at"
737 " line %d, entry \"%s\"\n"),
738 prog, linenum+i, dn );
739 rc = LDAP_PARAM_ERROR;
743 /* If prev op was deferred and matches this type,
746 if ( (mops[i-1] & LDAP_MOD_BVALUES)
747 && BV_CASEMATCH( btype+i, btype+i-1 ))
755 #if 0 /* we should faithfully encode the LDIF, not combine */
756 /* Make sure all modops with multiple values are contiguous */
757 for (i=idn; i<lines; i++) {
758 if ( mops[i] == M_SEP )
760 for (j=i+1; j<lines; j++) {
761 if ( mops[j] == M_SEP || mops[i] != mops[j] )
763 if ( BV_CASEMATCH( btype+i, btype+j )) {
765 /* out of order, move intervening attributes down */
774 for (k=j; k>i; k--) {
775 btype[k] = btype[k-1];
777 freeval[k] = freeval[k-1];
792 /* Allocate space for array of mods, array of pointers to mods,
793 * and array of pointers to values, allowing for NULL terminators
794 * for the pointer arrays...
796 lm = ber_memalloc( nmods * sizeof(LDAPMod) +
797 (nmods+1) * sizeof(LDAPMod*) +
798 (lines + nmods - idn) * sizeof(struct berval *));
799 pmods = (LDAPMod **)(lm+nmods);
800 bvl = (struct berval **)(pmods+nmods+1);
806 for (i=idn; i<lines; i++) {
807 if ( mops[i] == M_SEP )
809 if ( mops[i] != mops[i-1] || !BV_CASEMATCH( btype+i, &bv )) {
812 lm[j].mod_op = mops[i] | LDAP_MOD_BVALUES;
813 lm[j].mod_type = bv.bv_val;
814 if ( mops[i] & LDAP_MOD_BVALUES ) {
815 lm[j].mod_bvalues = NULL;
817 lm[j].mod_bvalues = bvl+k;
828 /* If default controls are set (as with -M option) and controls are
829 specified in the LDIF file, we must add the default controls to
830 the list of controls sent with the ldap operation.
834 LDAPControl **defctrls = NULL; /* Default server controls */
835 LDAPControl **newctrls = NULL;
836 ldap_get_option(ld, LDAP_OPT_SERVER_CONTROLS, &defctrls);
838 int npc=0; /* Num of LDIF controls */
839 int ndefc=0; /* Num of default controls */
840 while (pctrls[npc]) npc++; /* Count LDIF controls */
841 while (defctrls[ndefc]) ndefc++; /* Count default controls */
842 newctrls = ber_memrealloc(pctrls,
843 (npc+ndefc+1)*sizeof(LDAPControl*));
845 if (newctrls == NULL) {
850 for (i=npc; i<npc+ndefc; i++) {
851 pctrls[i] = ldap_control_dup(defctrls[i-npc]);
852 if (pctrls[i] == NULL) {
857 pctrls[npc+ndefc] = NULL;
859 ldap_controls_free(defctrls); /* Must be freed by library */
865 if ( delete_entry ) {
866 rc = dodelete( dn, pctrls );
867 } else if ( newrdn != NULL ) {
868 rc = dorename( dn, newrdn, newsup, deleteoldrdn, pctrls );
870 rc = domodify( dn, pmods, pctrls, new_entry );
873 if ( rc == LDAP_SUCCESS ) {
879 if (pctrls != NULL) {
880 ldap_controls_free( pctrls );
885 if ( mops != NULL ) {
888 for (i=lines-1; i>=0; i--)
889 if ( freeval[i] ) ber_memfree( vals[i].bv_val );
890 ber_memfree( btype );
895 /* Parse an LDIF control line of the form
896 control: oid [true/false] [: value] or
897 control: oid [true/false] [:: base64-value] or
898 control: oid [true/false] [:< url]
899 The control is added to the list of controls in *ppctrls.
904 LDAPControl ***ppctrls )
907 int criticality = 0; /* Default is false if not present */
910 LDAPControl *newctrl = NULL;
911 LDAPControl **pctrls = NULL;
912 struct berval type, bv;
915 if (ppctrls) pctrls = *ppctrls;
916 /* OID should come first. Validate and extract it. */
918 if (*s == 0) return ( LDAP_PARAM_ERROR );
920 while (isdigit((unsigned char)*s) || *s == '.') {
921 s++; /* OID should be digits or . */
924 return ( LDAP_PARAM_ERROR ); /* OID was not present */
926 if (*s) { /* End of OID should be space or NULL */
927 if (!isspace((unsigned char)*s)) {
928 return ( LDAP_PARAM_ERROR ); /* else OID contained invalid chars */
930 *s++ = 0; /* Replace space with null to terminate */
933 oid = ber_strdup(oidStart);
934 if (oid == NULL) return ( LDAP_NO_MEMORY );
936 /* Optional Criticality field is next. */
937 while (*s && isspace((unsigned char)*s)) {
938 s++; /* Skip white space before criticality */
940 if (strncasecmp(s, "true", 4) == 0) {
944 else if (strncasecmp(s, "false", 5) == 0) {
949 /* Optional value field is next */
950 while (*s && isspace((unsigned char)*s)) {
951 s++; /* Skip white space before value */
954 if (*s != ':') { /* If value is present, must start with : */
955 rc = LDAP_PARAM_ERROR;
959 /* Back up so value is in the form
963 Then we can use ldif_parse_line2 to extract and decode the value
968 rc = ldif_parse_line2(s, &type, &bv, &freeval);
970 rc = LDAP_PARAM_ERROR;
975 /* Create a new LDAPControl structure. */
976 newctrl = (LDAPControl *)ber_memalloc(sizeof(LDAPControl));
977 if ( newctrl == NULL ) {
981 newctrl->ldctl_oid = oid;
983 newctrl->ldctl_iscritical = criticality;
985 newctrl->ldctl_value = bv;
987 ber_dupbv( &newctrl->ldctl_value, &bv );
989 /* Add the new control to the passed-in list of controls. */
992 while ( pctrls[i] ) { /* Count the # of controls passed in */
996 /* Allocate 1 more slot for the new control and 1 for the NULL. */
997 pctrls = (LDAPControl **) ber_memrealloc(pctrls,
998 (i+2)*(sizeof(LDAPControl *)));
999 if (pctrls == NULL) {
1000 rc = LDAP_NO_MEMORY;
1003 pctrls[i] = newctrl;
1010 if (newctrl->ldctl_oid) ber_memfree(newctrl->ldctl_oid);
1011 if (newctrl->ldctl_value.bv_val) {
1012 ber_memfree(newctrl->ldctl_value.bv_val);
1014 ber_memfree(newctrl);
1016 if (oid) ber_memfree(oid);
1026 LDAPControl **pctrls,
1029 int rc, i, j, k, notascii, op;
1033 fprintf( stderr, _("%s: no DN specified\n"), prog );
1034 return( LDAP_PARAM_ERROR );
1037 if ( pmods == NULL ) {
1038 /* implement "touch" (empty sequence)
1039 * modify operation (note that there
1040 * is no symmetry with the UNIX command,
1041 * since \"touch\" on a non-existent entry
1043 printf( "warning: no attributes to %sadd (entry=\"%s\")\n",
1044 newentry ? "" : "change or ", dn );
1047 for ( i = 0; pmods[ i ] != NULL; ++i ) {
1048 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1049 if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
1051 _("%s: attribute \"%s\" has no values (entry=\"%s\")\n"),
1052 prog, pmods[i]->mod_type, dn );
1053 return LDAP_PARAM_ERROR;
1058 for ( i = 0; pmods[ i ] != NULL; ++i ) {
1059 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1061 op == LDAP_MOD_REPLACE ? _("replace") :
1062 op == LDAP_MOD_ADD ? _("add") :
1063 op == LDAP_MOD_INCREMENT ? _("increment") :
1064 op == LDAP_MOD_DELETE ? _("delete") :
1066 pmods[ i ]->mod_type );
1068 if ( pmods[ i ]->mod_bvalues != NULL ) {
1069 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1070 bvp = pmods[ i ]->mod_bvalues[ j ];
1072 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
1073 if ( !isascii( bvp->bv_val[ k ] )) {
1079 printf( _("\tNOT ASCII (%ld bytes)\n"), bvp->bv_len );
1081 printf( "\t%s\n", bvp->bv_val );
1090 printf( "%sadding new entry \"%s\"\n", dont ? "!" : "", dn );
1092 printf( "%smodifying entry \"%s\"\n", dont ? "!" : "", dn );
1098 rc = ldap_add_ext( ld, dn, pmods, pctrls, NULL, &msgid );
1100 rc = ldap_modify_ext( ld, dn, pmods, pctrls, NULL, &msgid );
1103 if ( rc != LDAP_SUCCESS ) {
1104 /* print error message about failed update including DN */
1105 fprintf( stderr, _("%s: update failed: %s\n"), prog, dn );
1106 tool_perror( newentry ? "ldap_add" : "ldap_modify",
1107 rc, NULL, NULL, NULL, NULL );
1110 rc = process_response( ld, msgid,
1111 newentry ? LDAP_RES_ADD : LDAP_RES_MODIFY, dn );
1113 if ( verbose && rc == LDAP_SUCCESS ) {
1114 printf( _("modify complete\n") );
1130 LDAPControl **pctrls )
1135 printf( _("%sdeleting entry \"%s\"\n"), dont ? "!" : "", dn );
1137 rc = ldap_delete_ext( ld, dn, pctrls, NULL, &msgid );
1138 if ( rc != LDAP_SUCCESS ) {
1139 fprintf( stderr, _("%s: delete failed: %s\n"), prog, dn );
1140 tool_perror( "ldap_delete", rc, NULL, NULL, NULL, NULL );
1143 rc = process_response( ld, msgid, LDAP_RES_DELETE, dn );
1145 if ( verbose && rc == LDAP_SUCCESS ) {
1146 printf( _("delete complete\n") );
1164 LDAPControl **pctrls )
1169 printf( _("%smodifying rdn of entry \"%s\"\n"), dont ? "!" : "", dn );
1171 printf( _("\tnew RDN: \"%s\" (%skeep existing values)\n"),
1172 newrdn, deleteoldrdn ? _("do not ") : "" );
1175 rc = ldap_rename( ld, dn, newrdn, newsup, deleteoldrdn,
1176 pctrls, NULL, &msgid );
1177 if ( rc != LDAP_SUCCESS ) {
1178 fprintf( stderr, _("%s: rename failed: %s\n"), prog, dn );
1179 tool_perror( "ldap_rename", rc, NULL, NULL, NULL, NULL );
1182 rc = process_response( ld, msgid, LDAP_RES_RENAME, dn );
1184 if ( verbose && rc == LDAP_SUCCESS ) {
1185 printf( _("rename complete\n") );
1197 res2str( int res ) {
1201 case LDAP_RES_DELETE:
1202 return "ldap_delete";
1203 case LDAP_RES_MODIFY:
1204 return "ldap_modify";
1205 case LDAP_RES_MODRDN:
1206 return "ldap_rename";
1211 return "ldap_unknown";
1214 static int process_response(
1221 int rc = LDAP_OTHER, msgtype;
1222 struct timeval tv = { 0, 0 };
1224 char *text = NULL, *matched = NULL, **refs = NULL;
1225 LDAPControl **ctrls = NULL;
1229 tv.tv_usec = 100000;
1231 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
1232 if ( tool_check_abandon( ld, msgid ) ) {
1233 return LDAP_CANCELLED;
1237 ldap_get_option( ld, LDAP_OPT_RESULT_CODE, &rc );
1238 tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
1247 msgtype = ldap_msgtype( res );
1249 rc = ldap_parse_result( ld, res, &err, &matched, &text, &refs, &ctrls, 1 );
1250 if ( rc == LDAP_SUCCESS ) rc = err;
1253 if ( rc == LDAP_X_TXN_SPECIFY_OKAY ) {
1257 if ( rc != LDAP_SUCCESS ) {
1258 tool_perror( res2str( op ), rc, NULL, matched, text, refs );
1259 } else if ( msgtype != op ) {
1260 fprintf( stderr, "%s: msgtype: expected %d got %d\n",
1261 res2str( op ), op, msgtype );
1265 if ( text ) ldap_memfree( text );
1266 if ( matched ) ldap_memfree( matched );
1267 if ( text ) ber_memvfree( (void **)refs );
1270 tool_print_ctrls( ld, ctrls );
1271 ldap_controls_free( ctrls );