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, _(" -c continuous operation mode (do not stop on errors)\n"));
141 fprintf( stderr, _(" -E [!]ext=extparam modify extensions"
142 " (! indicate s criticality)\n"));
143 fprintf( stderr, _(" -f file read operations from `file'\n"));
144 fprintf( stderr, _(" -M enable Manage DSA IT control (-MM to make critical)\n"));
145 fprintf( stderr, _(" -P version protocol version (default: 3)\n"));
148 _(" [!]txn=<commit|abort> (transaction)\n"));
150 fprintf( stderr, _(" -S file write skipped modifications to `file'\n"));
153 exit( EXIT_FAILURE );
157 const char options[] = "aE:rS:"
158 "cd:D:e:f:h:H:IMnNO:o:p:P:QR:U:vVw:WxX:y:Y:Z";
161 handle_private_option( int i )
163 char *control, *cvalue;
167 case 'E': /* modify extensions */
168 if( protocol == LDAP_VERSION2 ) {
169 fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
171 exit( EXIT_FAILURE );
174 /* should be extended to support comma separated list of
175 * [!]key[=value] parameters, e.g. -E !foo,bar=567
180 if( optarg[0] == '!' ) {
185 control = ber_strdup( optarg );
186 if ( (cvalue = strchr( control, '=' )) != NULL ) {
191 if( strcasecmp( control, "txn" ) == 0 ) {
195 _("txn control previously specified\n"));
196 exit( EXIT_FAILURE );
198 if( cvalue != NULL ) {
199 if( strcasecmp( cvalue, "abort" ) == 0 ) {
201 } else if( strcasecmp( cvalue, "commit" ) != 0 ) {
202 fprintf( stderr, _("Invalid value for txn control, %s\n"),
204 exit( EXIT_FAILURE );
212 fprintf( stderr, _("Invalid modify extension name: %s\n"),
222 case 'r': /* replace (obsolete) */
225 case 'S': /* skipped modifications to file */
226 if( rejfile != NULL ) {
227 fprintf( stderr, _("%s: -S previously specified\n"), prog );
228 exit( EXIT_FAILURE );
230 rejfile = ber_strdup( optarg );
241 main( int argc, char **argv )
243 char *rbuf = NULL, *rejbuf = NULL;
245 struct LDIFFP *ldiffp, ldifdummy = {0};
246 char *matched_msg, *error_msg;
247 int rc, retval, ldifrc;
250 int lineno, nextline = 0, lmax = 0;
253 prog = lutil_progname( "ldapmodify", argc, argv );
255 /* strncmp instead of strcmp since NT binaries carry .exe extension */
256 ldapadd = ( strncasecmp( prog, "ldapadd", sizeof("ldapadd")-1 ) == 0 );
258 tool_init( ldapadd ? TOOL_ADD : TOOL_MODIFY );
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 (( ldiffp = ldif_open( infile, "r" )) == NULL ) {
276 return( EXIT_FAILURE );
279 ldifdummy.fp = stdin;
283 if ( debug ) ldif_debug = debug;
285 ld = tool_conn_setup( dont, 0 );
288 if ( pw_file || want_bindpw ) {
290 rc = lutil_get_filed_password( pw_file, &passwd );
291 if( rc ) return EXIT_FAILURE;
293 passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
294 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
302 /* start transaction */
303 rc = ldap_txn_start_s( ld, NULL, NULL, &txn_id );
304 if( rc != LDAP_SUCCESS ) {
305 tool_perror( "ldap_txn_start_s", rc, NULL, NULL, NULL, NULL );
306 if( txn > 1 ) return EXIT_FAILURE;
320 c[i].ldctl_oid = LDAP_CONTROL_X_TXN_SPEC;
321 c[i].ldctl_value = *txn_id;
322 c[i].ldctl_iscritical = 1;
328 tool_server_controls( ld, c, i );
333 while (( rc == 0 || contoper ) && ( ldifrc = ldif_read_record( ldiffp, &nextline,
334 &rbuf, &lmax )) > 0 )
337 len = strlen( rbuf );
338 if (( rejbuf = (char *)ber_memalloc( len+1 )) == NULL ) {
340 exit( EXIT_FAILURE );
342 memcpy( rejbuf, rbuf, len+1 );
345 rc = process_ldif_rec( rbuf, lineno );
348 if ( rc ) retval = rc;
350 fprintf(rejfp, _("# Error: %s (%d)"), ldap_err2string(rc), rc);
353 ldap_get_option(ld, LDAP_OPT_MATCHED_DN, &matched_msg);
354 if ( matched_msg != NULL ) {
355 if ( *matched_msg != '\0' ) {
356 fprintf( rejfp, _(", matched DN: %s"), matched_msg );
358 ldap_memfree( matched_msg );
362 ldap_get_option(ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, &error_msg);
363 if ( error_msg != NULL ) {
364 if ( *error_msg != '\0' ) {
365 fprintf( rejfp, _(", additional info: %s"), error_msg );
367 ldap_memfree( error_msg );
369 fprintf( rejfp, "\n%s\n", rejbuf );
372 if (rejfp) ber_memfree( rejbuf );
380 if( retval == 0 && txn ) {
381 rc = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
382 if ( rc != LDAP_OPT_SUCCESS ) {
383 fprintf( stderr, "Could not unset controls for ldap_txn_end\n");
386 /* create transaction */
387 rc = ldap_txn_end_s( ld, !txnabort, txn_id, NULL, NULL, NULL );
388 if( rc != LDAP_SUCCESS ) {
389 tool_perror( "ldap_txn_end_s", rc, NULL, NULL, NULL, NULL );
399 if ( rejfp != NULL ) {
409 process_ldif_rec( char *rbuf, int linenum )
411 char *line, *dn, *newrdn, *newsup;
413 int expect_modop, expect_sep;
415 int new_entry, delete_entry, got_all;
416 LDAPMod **pmods, *lm = NULL;
418 LDAPControl **pctrls;
419 int i, j, k, lines, idn, nmods;
420 struct berval *btype, *vals, **bvl, bv;
422 unsigned char *mops = NULL;
426 rc = got_all = delete_entry = modop = expect_modop = 0;
432 dn = newrdn = newsup = NULL;
434 lines = ldif_countlines( rbuf );
435 btype = ber_memcalloc( 1, (lines+1)*2*sizeof(struct berval)+lines );
437 return LDAP_NO_MEMORY;
439 vals = btype+lines+1;
440 freeval = (char *)(vals+lines+1);
443 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
446 if ( *line == '\n' || *line == '\0' ) {
452 if ( line[0] == '-' && !line[1] ) {
453 BER_BVZERO( btype+i );
458 if ( ( rc = ldif_parse_line2( line, btype+i, vals+i, &freev ) ) < 0 ) {
459 fprintf( stderr, _("%s: invalid format (line %d) entry: \"%s\"\n"),
460 prog, linenum+i, dn == NULL ? "" : dn );
461 rc = LDAP_PARAM_ERROR;
467 if ( linenum+i == 1 && BV_CASEMATCH( btype+i, &BV_VERSION )) {
469 if( vals[i].bv_len == 0 || lutil_atoi( &v, vals[i].bv_val) != 0 || v != 1 ) {
471 _("%s: invalid version %s, line %d (ignored)\n"),
472 prog, vals[i].bv_val, linenum );
476 } else if ( BV_CASEMATCH( btype+i, &BV_DN )) {
480 /* skip all lines until we see "dn:" */
484 /* check to make sure there was a dn: line */
497 if( version && lines == 1 ) {
503 /* Check for "control" tag after dn and before changetype. */
504 if ( BV_CASEMATCH( btype+i, &BV_CONTROL )) {
505 /* Parse and add it to the list of controls */
506 rc = parse_ldif_control( vals+i, &pctrls );
509 _("%s: Error processing %s line, line %d: %s\n"),
510 prog, BV_CONTROL.bv_val, linenum+i, ldap_err2string(rc) );
516 _("%s: Expecting more input after %s line, line %d\n"),
517 prog, btype[i-1].bv_val, linenum+i );
519 rc = LDAP_PARAM_ERROR;
524 /* Check for changetype */
525 if ( BV_CASEMATCH( btype+i, &BV_CHANGETYPE )) {
526 #ifdef LIBERAL_CHANGETYPE_MODOP
527 /* trim trailing spaces (and log warning ...) */
529 for ( icnt = vals[i].bv_len; --icnt > 0; ) {
530 if ( !isspace( (unsigned char) vals[i].bv_val[icnt] ) ) {
535 if ( ++icnt != vals[i].bv_len ) {
536 fprintf( stderr, _("%s: illegal trailing space after"
537 " \"%s: %s\" trimmed (line %d, entry \"%s\")\n"),
538 prog, BV_CHANGETYPE.bv_val, vals[i].bv_val, linenum+i, dn );
539 vals[i].bv_val[icnt] = '\0';
541 #endif /* LIBERAL_CHANGETYPE_MODOP */
543 if ( BV_CASEMATCH( vals+i, &BV_MODIFYCT )) {
546 } else if ( BV_CASEMATCH( vals+i, &BV_ADDCT )) {
548 modop = LDAP_MOD_ADD;
549 } else if ( BV_CASEMATCH( vals+i, &BV_MODRDNCT )
550 || BV_CASEMATCH( vals+i, &BV_MODDNCT )
551 || BV_CASEMATCH( vals+i, &BV_RENAMECT ))
556 if ( !BV_CASEMATCH( btype+i, &BV_NEWRDN )) {
557 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
558 " \"%s:\" (line %d, entry \"%s\")\n"),
559 prog, BV_NEWRDN.bv_val, btype[i].bv_val, linenum+i, dn );
560 rc = LDAP_PARAM_ERROR;
563 newrdn = vals[i].bv_val;
567 if ( !BV_CASEMATCH( btype+i, &BV_DELETEOLDRDN )) {
568 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
569 " \"%s:\" (line %d, entry \"%s\")\n"),
570 prog, BV_DELETEOLDRDN.bv_val, btype[i].bv_val, linenum+i, dn );
571 rc = LDAP_PARAM_ERROR;
574 deleteoldrdn = ( vals[i].bv_val[0] == '0' ) ? 0 : 1;
577 if ( !BV_CASEMATCH( btype+i, &BV_NEWSUP )) {
578 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
579 " \"%s:\" (line %d, entry \"%s\")\n"),
580 prog, BV_NEWSUP.bv_val, btype[i].bv_val, linenum+i, dn );
581 rc = LDAP_PARAM_ERROR;
584 newsup = vals[i].bv_val;
588 } else if ( BV_CASEMATCH( vals+i, &BV_DELETECT )) {
589 got_all = delete_entry = 1;
592 _("%s: unknown %s \"%s\" (line %d, entry \"%s\")\n"),
593 prog, BV_CHANGETYPE.bv_val, vals[i].bv_val, linenum+i, dn );
594 rc = LDAP_PARAM_ERROR;
598 } else if ( ldapadd ) { /* missing changetype => add */
600 modop = LDAP_MOD_ADD;
602 expect_modop = 1; /* missing changetype => modify */
608 _("%s: extra lines at end (line %d, entry \"%s\")\n"),
609 prog, linenum+i, dn );
610 rc = LDAP_PARAM_ERROR;
622 /* Make sure all attributes with multiple values are contiguous */
623 for (; i<lines; i++) {
624 for (j=i+1; j<lines; j++) {
625 if ( BV_CASEMATCH( btype+i, btype+j )) {
627 /* out of order, move intervening attributes down */
631 for (k=j; k>i; k--) {
632 btype[k] = btype[k-1];
634 freeval[k] = freeval[k-1];
645 /* Allocate space for array of mods, array of pointers to mods,
646 * and array of pointers to values, allowing for NULL terminators
647 * for the pointer arrays...
649 lm = ber_memalloc( nmods * sizeof(LDAPMod) +
650 (nmods+1) * sizeof(LDAPMod*) +
651 (lines + nmods - idn) * sizeof(struct berval *));
652 pmods = (LDAPMod **)(lm+nmods);
653 bvl = (struct berval **)(pmods+nmods+1);
658 for (i=idn; i<lines; i++) {
659 if ( BV_CASEMATCH( btype+i, &BV_DN )) {
660 fprintf( stderr, _("%s: attributeDescription \"%s\":"
661 " (possible missing newline"
662 " after line %d, entry \"%s\"?)\n"),
663 prog, btype[i].bv_val, linenum+i - 1, dn );
665 if ( !BV_CASEMATCH( btype+i, &bv )) {
668 lm[j].mod_op = LDAP_MOD_ADD | LDAP_MOD_BVALUES;
669 lm[j].mod_type = bv.bv_val;
670 lm[j].mod_bvalues = bvl+k;
681 mops = ber_memalloc( lines+1 );
685 for ( ; i<lines; i++ ) {
686 if ( expect_modop ) {
687 #ifdef LIBERAL_CHANGETYPE_MODOP
688 /* trim trailing spaces (and log warning ...) */
690 for ( icnt = vals[i].bv_len; --icnt > 0; ) {
691 if ( !isspace( (unsigned char) vals[i].bv_val[icnt] ) ) break;
694 if ( ++icnt != vals[i].bv_len ) {
695 fprintf( stderr, _("%s: illegal trailing space after"
696 " \"%s: %s\" trimmed (line %d, entry \"%s\")\n"),
697 prog, type, vals[i].bv_val, linenum+i, dn );
698 vals[i].bv_val[icnt] = '\0';
700 #endif /* LIBERAL_CHANGETYPE_MODOP */
704 if ( BV_CASEMATCH( btype+i, &BV_MODOPADD )) {
705 modop = LDAP_MOD_ADD;
708 } else if ( BV_CASEMATCH( btype+i, &BV_MODOPREPLACE )) {
709 /* defer handling these since they might have no values.
710 * Use the BVALUES flag to signal that these were
711 * deferred. If values are provided later, this
712 * flag will be switched off.
714 modop = LDAP_MOD_REPLACE;
715 mops[i] = modop | LDAP_MOD_BVALUES;
717 } else if ( BV_CASEMATCH( btype+i, &BV_MODOPDELETE )) {
718 modop = LDAP_MOD_DELETE;
719 mops[i] = modop | LDAP_MOD_BVALUES;
721 } else if ( BV_CASEMATCH( btype+i, &BV_MODOPINCREMENT )) {
722 modop = LDAP_MOD_INCREMENT;
725 } else { /* no modify op: invalid LDIF */
726 fprintf( stderr, _("%s: modify operation type is missing at"
727 " line %d, entry \"%s\"\n"),
728 prog, linenum+i, dn );
729 rc = LDAP_PARAM_ERROR;
733 } else if ( expect_sep && BER_BVISEMPTY( btype+i )) {
739 if ( !BV_CASEMATCH( btype+i, &bv )) {
740 fprintf( stderr, _("%s: wrong attributeType at"
741 " line %d, entry \"%s\"\n"),
742 prog, linenum+i, dn );
743 rc = LDAP_PARAM_ERROR;
747 /* If prev op was deferred and matches this type,
750 if ( (mops[i-1] & LDAP_MOD_BVALUES)
751 && BV_CASEMATCH( btype+i, btype+i-1 ))
759 #if 0 /* we should faithfully encode the LDIF, not combine */
760 /* Make sure all modops with multiple values are contiguous */
761 for (i=idn; i<lines; i++) {
762 if ( mops[i] == M_SEP )
764 for (j=i+1; j<lines; j++) {
765 if ( mops[j] == M_SEP || mops[i] != mops[j] )
767 if ( BV_CASEMATCH( btype+i, btype+j )) {
769 /* out of order, move intervening attributes down */
778 for (k=j; k>i; k--) {
779 btype[k] = btype[k-1];
781 freeval[k] = freeval[k-1];
796 /* Allocate space for array of mods, array of pointers to mods,
797 * and array of pointers to values, allowing for NULL terminators
798 * for the pointer arrays...
800 lm = ber_memalloc( nmods * sizeof(LDAPMod) +
801 (nmods+1) * sizeof(LDAPMod*) +
802 (lines + nmods - idn) * sizeof(struct berval *));
803 pmods = (LDAPMod **)(lm+nmods);
804 bvl = (struct berval **)(pmods+nmods+1);
810 for (i=idn; i<lines; i++) {
811 if ( mops[i] == M_SEP )
813 if ( mops[i] != mops[i-1] || !BV_CASEMATCH( btype+i, &bv )) {
816 lm[j].mod_op = mops[i] | LDAP_MOD_BVALUES;
817 lm[j].mod_type = bv.bv_val;
818 if ( mops[i] & LDAP_MOD_BVALUES ) {
819 lm[j].mod_bvalues = NULL;
821 lm[j].mod_bvalues = bvl+k;
832 /* If default controls are set (as with -M option) and controls are
833 specified in the LDIF file, we must add the default controls to
834 the list of controls sent with the ldap operation.
838 LDAPControl **defctrls = NULL; /* Default server controls */
839 LDAPControl **newctrls = NULL;
840 ldap_get_option(ld, LDAP_OPT_SERVER_CONTROLS, &defctrls);
842 int npc=0; /* Num of LDIF controls */
843 int ndefc=0; /* Num of default controls */
844 while (pctrls[npc]) npc++; /* Count LDIF controls */
845 while (defctrls[ndefc]) ndefc++; /* Count default controls */
846 newctrls = ber_memrealloc(pctrls,
847 (npc+ndefc+1)*sizeof(LDAPControl*));
849 if (newctrls == NULL) {
854 for (i=npc; i<npc+ndefc; i++) {
855 pctrls[i] = ldap_control_dup(defctrls[i-npc]);
856 if (pctrls[i] == NULL) {
861 pctrls[npc+ndefc] = NULL;
863 ldap_controls_free(defctrls); /* Must be freed by library */
869 if ( delete_entry ) {
870 rc = dodelete( dn, pctrls );
871 } else if ( newrdn != NULL ) {
872 rc = dorename( dn, newrdn, newsup, deleteoldrdn, pctrls );
874 rc = domodify( dn, pmods, pctrls, new_entry );
877 if ( rc == LDAP_SUCCESS ) {
883 if (pctrls != NULL) {
884 ldap_controls_free( pctrls );
889 if ( mops != NULL ) {
892 for (i=lines-1; i>=0; i--)
893 if ( freeval[i] ) ber_memfree( vals[i].bv_val );
894 ber_memfree( btype );
899 /* Parse an LDIF control line of the form
900 control: oid [true/false] [: value] or
901 control: oid [true/false] [:: base64-value] or
902 control: oid [true/false] [:< url]
903 The control is added to the list of controls in *ppctrls.
908 LDAPControl ***ppctrls )
911 int criticality = 0; /* Default is false if not present */
914 LDAPControl *newctrl = NULL;
915 LDAPControl **pctrls = NULL;
916 struct berval type, bv;
919 if (ppctrls) pctrls = *ppctrls;
920 /* OID should come first. Validate and extract it. */
922 if (*s == 0) return ( LDAP_PARAM_ERROR );
924 while (isdigit((unsigned char)*s) || *s == '.') {
925 s++; /* OID should be digits or . */
928 return ( LDAP_PARAM_ERROR ); /* OID was not present */
930 if (*s) { /* End of OID should be space or NULL */
931 if (!isspace((unsigned char)*s)) {
932 return ( LDAP_PARAM_ERROR ); /* else OID contained invalid chars */
934 *s++ = 0; /* Replace space with null to terminate */
937 oid = ber_strdup(oidStart);
938 if (oid == NULL) return ( LDAP_NO_MEMORY );
940 /* Optional Criticality field is next. */
941 while (*s && isspace((unsigned char)*s)) {
942 s++; /* Skip white space before criticality */
944 if (strncasecmp(s, "true", 4) == 0) {
948 else if (strncasecmp(s, "false", 5) == 0) {
953 /* Optional value field is next */
954 while (*s && isspace((unsigned char)*s)) {
955 s++; /* Skip white space before value */
958 if (*s != ':') { /* If value is present, must start with : */
959 rc = LDAP_PARAM_ERROR;
963 /* Back up so value is in the form
967 Then we can use ldif_parse_line2 to extract and decode the value
972 rc = ldif_parse_line2(s, &type, &bv, &freeval);
974 rc = LDAP_PARAM_ERROR;
979 /* Create a new LDAPControl structure. */
980 newctrl = (LDAPControl *)ber_memalloc(sizeof(LDAPControl));
981 if ( newctrl == NULL ) {
985 newctrl->ldctl_oid = oid;
987 newctrl->ldctl_iscritical = criticality;
989 newctrl->ldctl_value = bv;
991 ber_dupbv( &newctrl->ldctl_value, &bv );
993 /* Add the new control to the passed-in list of controls. */
996 while ( pctrls[i] ) { /* Count the # of controls passed in */
1000 /* Allocate 1 more slot for the new control and 1 for the NULL. */
1001 pctrls = (LDAPControl **) ber_memrealloc(pctrls,
1002 (i+2)*(sizeof(LDAPControl *)));
1003 if (pctrls == NULL) {
1004 rc = LDAP_NO_MEMORY;
1007 pctrls[i] = newctrl;
1014 if (newctrl->ldctl_oid) ber_memfree(newctrl->ldctl_oid);
1015 if (newctrl->ldctl_value.bv_val) {
1016 ber_memfree(newctrl->ldctl_value.bv_val);
1018 ber_memfree(newctrl);
1020 if (oid) ber_memfree(oid);
1030 LDAPControl **pctrls,
1033 int rc, i, j, k, notascii, op;
1037 fprintf( stderr, _("%s: no DN specified\n"), prog );
1038 return( LDAP_PARAM_ERROR );
1041 if ( pmods == NULL ) {
1042 /* implement "touch" (empty sequence)
1043 * modify operation (note that there
1044 * is no symmetry with the UNIX command,
1045 * since \"touch\" on a non-existent entry
1047 printf( "warning: no attributes to %sadd (entry=\"%s\")\n",
1048 newentry ? "" : "change or ", dn );
1051 for ( i = 0; pmods[ i ] != NULL; ++i ) {
1052 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1053 if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
1055 _("%s: attribute \"%s\" has no values (entry=\"%s\")\n"),
1056 prog, pmods[i]->mod_type, dn );
1057 return LDAP_PARAM_ERROR;
1062 for ( i = 0; pmods[ i ] != NULL; ++i ) {
1063 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1065 op == LDAP_MOD_REPLACE ? _("replace") :
1066 op == LDAP_MOD_ADD ? _("add") :
1067 op == LDAP_MOD_INCREMENT ? _("increment") :
1068 op == LDAP_MOD_DELETE ? _("delete") :
1070 pmods[ i ]->mod_type );
1072 if ( pmods[ i ]->mod_bvalues != NULL ) {
1073 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1074 bvp = pmods[ i ]->mod_bvalues[ j ];
1076 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
1077 if ( !isascii( bvp->bv_val[ k ] )) {
1083 printf( _("\tNOT ASCII (%ld bytes)\n"), bvp->bv_len );
1085 printf( "\t%s\n", bvp->bv_val );
1094 printf( "%sadding new entry \"%s\"\n", dont ? "!" : "", dn );
1096 printf( "%smodifying entry \"%s\"\n", dont ? "!" : "", dn );
1102 rc = ldap_add_ext( ld, dn, pmods, pctrls, NULL, &msgid );
1104 rc = ldap_modify_ext( ld, dn, pmods, pctrls, NULL, &msgid );
1107 if ( rc != LDAP_SUCCESS ) {
1108 /* print error message about failed update including DN */
1109 fprintf( stderr, _("%s: update failed: %s\n"), prog, dn );
1110 tool_perror( newentry ? "ldap_add" : "ldap_modify",
1111 rc, NULL, NULL, NULL, NULL );
1114 rc = process_response( ld, msgid,
1115 newentry ? LDAP_RES_ADD : LDAP_RES_MODIFY, dn );
1117 if ( verbose && rc == LDAP_SUCCESS ) {
1118 printf( _("modify complete\n") );
1134 LDAPControl **pctrls )
1139 printf( _("%sdeleting entry \"%s\"\n"), dont ? "!" : "", dn );
1141 rc = ldap_delete_ext( ld, dn, pctrls, NULL, &msgid );
1142 if ( rc != LDAP_SUCCESS ) {
1143 fprintf( stderr, _("%s: delete failed: %s\n"), prog, dn );
1144 tool_perror( "ldap_delete", rc, NULL, NULL, NULL, NULL );
1147 rc = process_response( ld, msgid, LDAP_RES_DELETE, dn );
1149 if ( verbose && rc == LDAP_SUCCESS ) {
1150 printf( _("delete complete\n") );
1168 LDAPControl **pctrls )
1173 printf( _("%smodifying rdn of entry \"%s\"\n"), dont ? "!" : "", dn );
1175 printf( _("\tnew RDN: \"%s\" (%skeep existing values)\n"),
1176 newrdn, deleteoldrdn ? _("do not ") : "" );
1179 rc = ldap_rename( ld, dn, newrdn, newsup, deleteoldrdn,
1180 pctrls, NULL, &msgid );
1181 if ( rc != LDAP_SUCCESS ) {
1182 fprintf( stderr, _("%s: rename failed: %s\n"), prog, dn );
1183 tool_perror( "ldap_rename", rc, NULL, NULL, NULL, NULL );
1186 rc = process_response( ld, msgid, LDAP_RES_RENAME, dn );
1188 if ( verbose && rc == LDAP_SUCCESS ) {
1189 printf( _("rename complete\n") );
1201 res2str( int res ) {
1205 case LDAP_RES_DELETE:
1206 return "ldap_delete";
1207 case LDAP_RES_MODIFY:
1208 return "ldap_modify";
1209 case LDAP_RES_MODRDN:
1210 return "ldap_rename";
1215 return "ldap_unknown";
1218 static int process_response(
1225 int rc = LDAP_OTHER, msgtype;
1226 struct timeval tv = { 0, 0 };
1228 char *text = NULL, *matched = NULL, **refs = NULL;
1229 LDAPControl **ctrls = NULL;
1233 tv.tv_usec = 100000;
1235 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
1236 if ( tool_check_abandon( ld, msgid ) ) {
1237 return LDAP_CANCELLED;
1241 ldap_get_option( ld, LDAP_OPT_RESULT_CODE, &rc );
1242 tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
1251 msgtype = ldap_msgtype( res );
1253 rc = ldap_parse_result( ld, res, &err, &matched, &text, &refs, &ctrls, 1 );
1254 if ( rc == LDAP_SUCCESS ) rc = err;
1257 if ( rc == LDAP_X_TXN_SPECIFY_OKAY ) {
1261 if ( rc != LDAP_SUCCESS ) {
1262 tool_perror( res2str( op ), rc, NULL, matched, text, refs );
1263 } else if ( msgtype != op ) {
1264 fprintf( stderr, "%s: msgtype: expected %d got %d\n",
1265 res2str( op ), op, msgtype );
1269 if ( text ) ldap_memfree( text );
1270 if ( matched ) ldap_memfree( matched );
1271 if ( text ) ber_memvfree( (void **)refs );
1274 tool_print_ctrls( ld, ctrls );
1275 ldap_controls_free( ctrls );