1 /* ldapmodify.c - generic program to modify or add entries using LDAP */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2007 The OpenLDAP Foundation.
6 * Portions Copyright 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 int ldapadd, force = 0;
74 static char *rejfile = NULL;
75 static LDAP *ld = NULL;
79 /* strings found in replog/LDIF entries (mostly lifted from slurpd/slurp.h) */
80 static struct berval BV_VERSION = BER_BVC("version");
81 static struct berval BV_REPLICA = BER_BVC("replica");
82 static struct berval BV_DN = BER_BVC("dn");
83 static struct berval BV_CONTROL = BER_BVC("control");
84 static struct berval BV_CHANGETYPE = BER_BVC("changetype");
85 static struct berval BV_ADDCT = BER_BVC("add");
86 static struct berval BV_MODIFYCT = BER_BVC("modify");
87 static struct berval BV_DELETECT = BER_BVC("delete");
88 static struct berval BV_MODRDNCT = BER_BVC("modrdn");
89 static struct berval BV_MODDNCT = BER_BVC("moddn");
90 static struct berval BV_RENAMECT = BER_BVC("rename");
91 static struct berval BV_MODOPADD = BER_BVC("add");
92 static struct berval BV_MODOPREPLACE = BER_BVC("replace");
93 static struct berval BV_MODOPDELETE = BER_BVC("delete");
94 static struct berval BV_MODOPINCREMENT = BER_BVC("increment");
95 static struct berval BV_MODSEP = BER_BVC("-");
96 static struct berval BV_NEWRDN = BER_BVC("newrdn");
97 static struct berval BV_DELETEOLDRDN = BER_BVC("deleteoldrdn");
98 static struct berval BV_NEWSUP = BER_BVC("newsuperior");
100 #define BVICMP(a,b) ((a)->bv_len != (b)->bv_len ? \
101 (a)->bv_len - (b)->bv_len : strcasecmp((a)->bv_val, (b)->bv_val))
103 static int process_ldif_rec LDAP_P(( char *rbuf, int lineno ));
104 static int parse_ldif_control LDAP_P(( struct berval *val, LDAPControl ***pctrls ));
105 static int domodify LDAP_P((
108 LDAPControl **pctrls,
110 static int dodelete LDAP_P((
112 LDAPControl **pctrls ));
113 static int dorename LDAP_P((
118 LDAPControl **pctrls ));
119 static int process_response(
127 static int txnabort = 0;
128 struct berval *txn_id = NULL;
134 fprintf( stderr, _("Add or modify entries from an LDAP server\n\n"));
135 fprintf( stderr, _("usage: %s [options]\n"), prog);
136 fprintf( stderr, _(" The list of desired operations are read from stdin"
137 " or from the file\n"));
138 fprintf( stderr, _(" specified by \"-f file\".\n"));
139 fprintf( stderr, _("Add or modify options:\n"));
140 fprintf( stderr, _(" -a add values (%s)\n"),
141 (ldapadd ? _("default") : _("default is to replace")));
142 fprintf( stderr, _(" -E [!]ext=extparam modify extensions"
143 " (! indicate s criticality)\n"));
146 _(" [!]txn=<commit|abort> (transaction)\n"));
148 fprintf( stderr, _(" -F force all changes records to be used\n"));
149 fprintf( stderr, _(" -S file write skipped modifications to `file'\n"));
152 exit( EXIT_FAILURE );
156 const char options[] = "aE:FrS:"
157 "cd:D:e:f:h:H:IMnO: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 'F': /* force all changes records to be used */
225 case 'r': /* replace (obsolete) */
228 case 'S': /* skipped modifications to file */
229 if( rejfile != NULL ) {
230 fprintf( stderr, _("%s: -S previously specified\n"), prog );
231 exit( EXIT_FAILURE );
233 rejfile = ber_strdup( optarg );
244 main( int argc, char **argv )
246 char *rbuf = NULL, *rejbuf = NULL;
248 struct LDIFFP *ldiffp, ldifdummy = {0};
249 char *matched_msg, *error_msg;
253 int lineno, nextline = 0, lmax = 0;
256 prog = lutil_progname( "ldapmodify", argc, argv );
258 /* strncmp instead of strcmp since NT binaries carry .exe extension */
259 ldapadd = ( strncasecmp( prog, "ldapadd", sizeof("ldapadd")-1 ) == 0 );
261 tool_init( ldapadd ? TOOL_ADD : TOOL_MODIFY );
263 tool_args( argc, argv );
265 if ( argc != optind ) usage();
267 if ( rejfile != NULL ) {
268 if (( rejfp = fopen( rejfile, "w" )) == NULL ) {
270 return( EXIT_FAILURE );
276 if ( infile != NULL ) {
277 if (( ldiffp = ldif_open( infile, "r" )) == NULL ) {
279 return( EXIT_FAILURE );
282 ldifdummy.fp = stdin;
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;
305 /* start transaction */
306 rc = ldap_txn_start_s( ld, NULL, NULL, &txn_id );
307 if( rc != LDAP_SUCCESS ) {
308 tool_perror( "ldap_txn_start_s", rc, NULL, NULL, NULL, NULL );
309 if( txn > 1 ) return EXIT_FAILURE;
323 c[i].ldctl_oid = LDAP_CONTROL_X_TXN_SPEC;
324 c[i].ldctl_value = *txn_id;
325 c[i].ldctl_iscritical = 1;
331 tool_server_controls( ld, c, i );
336 while (( rc == 0 || contoper ) && ldif_read_record( ldiffp, &nextline,
340 len = strlen( rbuf );
341 if (( rejbuf = (char *)ber_memalloc( len+1 )) == NULL ) {
343 exit( EXIT_FAILURE );
345 memcpy( rejbuf, rbuf, len+1 );
348 rc = process_ldif_rec( rbuf, lineno );
351 if ( rc ) retval = rc;
353 fprintf(rejfp, _("# Error: %s (%d)"), ldap_err2string(rc), rc);
356 ldap_get_option(ld, LDAP_OPT_MATCHED_DN, &matched_msg);
357 if ( matched_msg != NULL ) {
358 if ( *matched_msg != '\0' ) {
359 fprintf( rejfp, _(", matched DN: %s"), matched_msg );
361 ldap_memfree( matched_msg );
365 ldap_get_option(ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, &error_msg);
366 if ( error_msg != NULL ) {
367 if ( *error_msg != '\0' ) {
368 fprintf( rejfp, _(", additional info: %s"), error_msg );
370 ldap_memfree( error_msg );
372 fprintf( rejfp, "\n%s\n", rejbuf );
375 if (rejfp) free( 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;
412 int rc, modop, replicaport;
413 int expect_modop, expect_sep;
415 int saw_replica, use_record, 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 = saw_replica = delete_entry = modop = expect_modop = 0;
433 dn = newrdn = newsup = NULL;
435 lines = ldif_countlines( rbuf );
436 btype = ber_memcalloc( 1, (lines+1)*2*sizeof(struct berval)+lines );
437 vals = btype+lines+1;
438 freeval = (char *)(vals+lines+1);
441 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
444 if ( *line == '\n' || *line == '\0' ) {
450 if ( line[0] == '-' && !line[1] ) {
451 BER_BVZERO( btype+i );
456 if ( ( rc = ldif_parse_line2( line, btype+i, vals+i, &freev ) ) < 0 ) {
457 fprintf( stderr, _("%s: invalid format (line %d) entry: \"%s\"\n"),
458 prog, linenum+i, dn == NULL ? "" : dn );
459 rc = LDAP_PARAM_ERROR;
465 if ( !use_record && !BVICMP( btype+i, &BV_REPLICA )) {
468 if (( p = strchr( vals[i].bv_val, ':' )) == NULL ) {
472 if ( lutil_atoi( &replicaport, p ) != 0 ) {
473 fprintf( stderr, _("%s: unable to parse replica port \"%s\" (line %d) entry: \"%s\"\n"),
474 prog, p, linenum+i, dn == NULL ? "" : dn );
475 rc = LDAP_PARAM_ERROR;
479 if ( ldaphost != NULL &&
480 strcasecmp( vals[i].bv_val, ldaphost ) == 0 &&
481 replicaport == ldapport )
485 } else if ( linenum+i == 1 && !BVICMP( btype+i, &BV_VERSION )) {
487 if( vals[i].bv_len == 0 || lutil_atoi( &v, vals[i].bv_val) != 0 || v != 1 ) {
489 _("%s: invalid version %s, line %d (ignored)\n"),
490 prog, vals[i].bv_val, linenum );
494 } else if ( !BVICMP( btype+i, &BV_DN )) {
497 if ( !use_record && saw_replica ) {
498 printf(_("%s: skipping change record for entry: %s at line %d\n"),
499 prog, dn, linenum+i);
500 printf(_("\t(LDAP host/port does not match replica: lines)\n"));
505 /* skip all lines until we see "dn:" */
509 /* check to make sure there was a dn: line */
522 if( version && lines == 1 ) {
528 /* Check for "control" tag after dn and before changetype. */
529 if (!BVICMP( btype+i, &BV_CONTROL)) {
530 /* Parse and add it to the list of controls */
531 rc = parse_ldif_control( vals+i, &pctrls );
534 _("%s: Error processing %s line, line %d: %s\n"),
535 prog, BV_CONTROL.bv_val, linenum+i, ldap_err2string(rc) );
541 _("%s: Expecting more input after %s line, line %d\n"),
542 prog, btype[i-1].bv_val, linenum+i );
544 rc = LDAP_PARAM_ERROR;
549 /* Check for changetype */
550 if ( !BVICMP( btype+i, &BV_CHANGETYPE )) {
551 #ifdef LIBERAL_CHANGETYPE_MODOP
552 /* trim trailing spaces (and log warning ...) */
554 for ( icnt = vals[i].bv_len; --icnt > 0; ) {
555 if ( !isspace( (unsigned char) vals[i].bv_val[icnt] ) ) {
560 if ( ++icnt != vals[i].bv_len ) {
561 fprintf( stderr, _("%s: illegal trailing space after"
562 " \"%s: %s\" trimmed (line %d, entry \"%s\")\n"),
563 prog, BV_CHANGETYPE.bv_val, vals[i].bv_val, linenum+i, dn );
564 vals[i].bv_val[icnt] = '\0';
566 #endif /* LIBERAL_CHANGETYPE_MODOP */
568 if ( BVICMP( vals+i, &BV_MODIFYCT ) == 0 ) {
571 } else if ( BVICMP( vals+i, &BV_ADDCT ) == 0 ) {
573 modop = LDAP_MOD_ADD;
574 } else if ( BVICMP( vals+i, &BV_MODRDNCT ) == 0
575 || BVICMP( vals+i, &BV_MODDNCT ) == 0
576 || BVICMP( vals+i, &BV_RENAMECT ) == 0)
581 if ( BVICMP( btype+i, &BV_NEWRDN )) {
582 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
583 " \"%s:\" (line %d, entry \"%s\")\n"),
584 prog, BV_NEWRDN.bv_val, btype[i].bv_val, linenum+i, dn );
585 rc = LDAP_PARAM_ERROR;
588 newrdn = vals[i].bv_val;
592 if ( BVICMP( btype+i, &BV_DELETEOLDRDN )) {
593 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
594 " \"%s:\" (line %d, entry \"%s\")\n"),
595 prog, BV_DELETEOLDRDN.bv_val, btype[i].bv_val, linenum+i, dn );
596 rc = LDAP_PARAM_ERROR;
599 deleteoldrdn = ( vals[i].bv_val[0] == '0' ) ? 0 : 1;
602 if ( BVICMP( btype+i, &BV_NEWSUP )) {
603 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
604 " \"%s:\" (line %d, entry \"%s\")\n"),
605 prog, BV_NEWSUP.bv_val, btype[i].bv_val, linenum+i, dn );
606 rc = LDAP_PARAM_ERROR;
609 newsup = vals[i].bv_val;
613 } else if ( BVICMP( vals+i, &BV_DELETECT ) == 0 ) {
614 got_all = delete_entry = 1;
617 _("%s: unknown %s \"%s\" (line %d, entry \"%s\")\n"),
618 prog, BV_CHANGETYPE.bv_val, vals[i].bv_val, linenum+i, dn );
619 rc = LDAP_PARAM_ERROR;
623 } else if ( ldapadd ) { /* missing changetype => add */
625 modop = LDAP_MOD_ADD;
627 expect_modop = 1; /* missing changetype => modify */
633 _("%s: extra lines at end (line %d, entry \"%s\")\n"),
634 prog, linenum+i, dn );
635 rc = LDAP_PARAM_ERROR;
647 /* Make sure all attributes with multiple values are contiguous */
648 for (; i<lines; i++) {
649 for (j=i+1; j<lines; j++) {
650 if ( !BVICMP( btype+i, btype+j )) {
652 /* out of order, move intervening attributes down */
656 for (k=j; k>i; k--) {
657 btype[k] = btype[k-1];
659 freeval[k] = freeval[k-1];
670 /* Allocate space for array of mods, array of pointers to mods,
671 * and array of pointers to values, allowing for NULL terminators
672 * for the pointer arrays...
674 lm = ber_memalloc( nmods * sizeof(LDAPMod) +
675 (nmods+1) * sizeof(LDAPMod*) +
676 (lines + nmods - idn) * sizeof(struct berval *));
677 pmods = (LDAPMod **)(lm+nmods);
678 bvl = (struct berval **)(pmods+nmods+1);
683 for (i=idn; i<lines; i++) {
684 if ( !BVICMP( btype+i, &BV_DN )) {
685 fprintf( stderr, _("%s: attributeDescription \"%s\":"
686 " (possible missing newline"
687 " after line %d, entry \"%s\"?)\n"),
688 prog, btype[i].bv_val, linenum+i - 1, dn );
690 if ( BVICMP(btype+i,&bv)) {
693 lm[j].mod_op = LDAP_MOD_ADD | LDAP_MOD_BVALUES;
694 lm[j].mod_type = bv.bv_val;
695 lm[j].mod_bvalues = bvl+k;
706 mops = ber_memalloc( lines+1 );
710 for ( ; i<lines; i++ ) {
711 if ( expect_modop ) {
712 #ifdef LIBERAL_CHANGETYPE_MODOP
713 /* trim trailing spaces (and log warning ...) */
715 for ( icnt = vals[i].bv_len; --icnt > 0; ) {
716 if ( !isspace( (unsigned char) vals[i].bv_val[icnt] ) ) break;
719 if ( ++icnt != vals[i].bv_len ) {
720 fprintf( stderr, _("%s: illegal trailing space after"
721 " \"%s: %s\" trimmed (line %d, entry \"%s\")\n"),
722 prog, type, vals[i].bv_val, linenum+i, dn );
723 vals[i].bv_val[icnt] = '\0';
725 #endif /* LIBERAL_CHANGETYPE_MODOP */
729 if ( BVICMP( btype+i, &BV_MODOPADD ) == 0 ) {
730 modop = LDAP_MOD_ADD;
733 } else if ( BVICMP( btype+i, &BV_MODOPREPLACE ) == 0 ) {
734 /* defer handling these since they might have no values.
735 * Use the BVALUES flag to signal that these were
736 * deferred. If values are provided later, this
737 * flag will be switched off.
739 modop = LDAP_MOD_REPLACE;
740 mops[i] = modop | LDAP_MOD_BVALUES;
742 } else if ( BVICMP( btype+i, &BV_MODOPDELETE ) == 0 ) {
743 modop = LDAP_MOD_DELETE;
744 mops[i] = modop | LDAP_MOD_BVALUES;
746 } else if ( BVICMP( btype+i, &BV_MODOPINCREMENT ) == 0 ) {
747 modop = LDAP_MOD_INCREMENT;
750 } else { /* no modify op: invalid LDIF */
751 fprintf( stderr, _("%s: modify operation type is missing at"
752 " line %d, entry \"%s\"\n"),
753 prog, linenum+i, dn );
754 rc = LDAP_PARAM_ERROR;
758 } else if ( expect_sep && BER_BVISEMPTY( btype+i )) {
764 if ( BVICMP( btype+i, &bv )) {
765 fprintf( stderr, _("%s: wrong attributeType at"
766 " line %d, entry \"%s\"\n"),
767 prog, linenum+i, dn );
768 rc = LDAP_PARAM_ERROR;
772 /* If prev op was deferred and matches this type,
775 if ( (mops[i-1]&LDAP_MOD_BVALUES) && !BVICMP(btype+i,
783 #if 0 /* we should faithfully encode the LDIF, not combine */
784 /* Make sure all modops with multiple values are contiguous */
785 for (i=idn; i<lines; i++) {
786 if ( mops[i] == M_SEP )
788 for (j=i+1; j<lines; j++) {
789 if ( mops[j] == M_SEP || mops[i] != mops[j] )
791 if ( !BVICMP( btype+i, btype+j )) {
793 /* out of order, move intervening attributes down */
802 for (k=j; k>i; k--) {
803 btype[k] = btype[k-1];
805 freeval[k] = freeval[k-1];
820 /* Allocate space for array of mods, array of pointers to mods,
821 * and array of pointers to values, allowing for NULL terminators
822 * for the pointer arrays...
824 lm = ber_memalloc( nmods * sizeof(LDAPMod) +
825 (nmods+1) * sizeof(LDAPMod*) +
826 (lines + nmods - idn) * sizeof(struct berval *));
827 pmods = (LDAPMod **)(lm+nmods);
828 bvl = (struct berval **)(pmods+nmods+1);
834 for (i=idn; i<lines; i++) {
835 if ( mops[i] == M_SEP )
837 if ( mops[i] != mops[i-1] || BVICMP(btype+i,&bv)) {
840 lm[j].mod_op = mops[i] | LDAP_MOD_BVALUES;
841 lm[j].mod_type = bv.bv_val;
842 if ( mops[i] & LDAP_MOD_BVALUES ) {
843 lm[j].mod_bvalues = NULL;
845 lm[j].mod_bvalues = bvl+k;
856 /* If default controls are set (as with -M option) and controls are
857 specified in the LDIF file, we must add the default controls to
858 the list of controls sent with the ldap operation.
862 LDAPControl **defctrls = NULL; /* Default server controls */
863 LDAPControl **newctrls = NULL;
864 ldap_get_option(ld, LDAP_OPT_SERVER_CONTROLS, &defctrls);
866 int npc=0; /* Num of LDIF controls */
867 int ndefc=0; /* Num of default controls */
868 while (pctrls[npc]) npc++; /* Count LDIF controls */
869 while (defctrls[ndefc]) ndefc++; /* Count default controls */
870 newctrls = ber_memrealloc(pctrls,
871 (npc+ndefc+1)*sizeof(LDAPControl*));
873 if (newctrls == NULL) {
878 for (i=npc; i<npc+ndefc; i++) {
879 pctrls[i] = ldap_control_dup(defctrls[i-npc]);
880 if (pctrls[i] == NULL) {
885 pctrls[npc+ndefc] = NULL;
887 ldap_controls_free(defctrls); /* Must be freed by library */
893 if ( delete_entry ) {
894 rc = dodelete( dn, pctrls );
895 } else if ( newrdn != NULL ) {
896 rc = dorename( dn, newrdn, newsup, deleteoldrdn, pctrls );
898 rc = domodify( dn, pmods, pctrls, new_entry );
901 if ( rc == LDAP_SUCCESS ) {
907 if (pctrls != NULL) {
908 ldap_controls_free( pctrls );
913 if ( mops != NULL ) {
916 for (i=lines-1; i>=0; i--)
917 if ( freeval[i] ) ber_memfree( vals[i].bv_val );
918 ber_memfree( btype );
923 /* Parse an LDIF control line of the form
924 control: oid [true/false] [: value] or
925 control: oid [true/false] [:: base64-value] or
926 control: oid [true/false] [:< url]
927 The control is added to the list of controls in *ppctrls.
932 LDAPControl ***ppctrls )
935 int criticality = 0; /* Default is false if not present */
938 LDAPControl *newctrl = NULL;
939 LDAPControl **pctrls = NULL;
940 struct berval type, bv;
943 if (ppctrls) pctrls = *ppctrls;
944 /* OID should come first. Validate and extract it. */
946 if (*s == 0) return ( LDAP_PARAM_ERROR );
948 while (isdigit((unsigned char)*s) || *s == '.') {
949 s++; /* OID should be digits or . */
952 return ( LDAP_PARAM_ERROR ); /* OID was not present */
954 if (*s) { /* End of OID should be space or NULL */
955 if (!isspace((unsigned char)*s)) {
956 return ( LDAP_PARAM_ERROR ); /* else OID contained invalid chars */
958 *s++ = 0; /* Replace space with null to terminate */
961 oid = ber_strdup(oidStart);
962 if (oid == NULL) return ( LDAP_NO_MEMORY );
964 /* Optional Criticality field is next. */
965 while (*s && isspace((unsigned char)*s)) {
966 s++; /* Skip white space before criticality */
968 if (strncasecmp(s, "true", 4) == 0) {
972 else if (strncasecmp(s, "false", 5) == 0) {
977 /* Optional value field is next */
978 while (*s && isspace((unsigned char)*s)) {
979 s++; /* Skip white space before value */
982 if (*s != ':') { /* If value is present, must start with : */
983 rc = LDAP_PARAM_ERROR;
987 /* Back up so value is in the form
991 Then we can use ldif_parse_line2 to extract and decode the value
996 rc = ldif_parse_line2(s, &type, &bv, &freeval);
998 rc = LDAP_PARAM_ERROR;
1003 /* Create a new LDAPControl structure. */
1004 newctrl = (LDAPControl *)ber_memalloc(sizeof(LDAPControl));
1005 if ( newctrl == NULL ) {
1006 rc = LDAP_NO_MEMORY;
1009 newctrl->ldctl_oid = oid;
1011 newctrl->ldctl_iscritical = criticality;
1013 newctrl->ldctl_value = bv;
1015 ber_dupbv( &newctrl->ldctl_value, &bv );
1017 /* Add the new control to the passed-in list of controls. */
1020 while ( pctrls[i] ) { /* Count the # of controls passed in */
1024 /* Allocate 1 more slot for the new control and 1 for the NULL. */
1025 pctrls = (LDAPControl **) ber_memrealloc(pctrls,
1026 (i+2)*(sizeof(LDAPControl *)));
1027 if (pctrls == NULL) {
1028 rc = LDAP_NO_MEMORY;
1031 pctrls[i] = newctrl;
1038 if (newctrl->ldctl_oid) ber_memfree(newctrl->ldctl_oid);
1039 if (newctrl->ldctl_value.bv_val) {
1040 ber_memfree(newctrl->ldctl_value.bv_val);
1042 ber_memfree(newctrl);
1044 if (oid) ber_memfree(oid);
1054 LDAPControl **pctrls,
1057 int rc, i, j, k, notascii, op;
1061 fprintf( stderr, _("%s: no DN specified\n"), prog );
1062 return( LDAP_PARAM_ERROR );
1065 if ( pmods == NULL ) {
1066 /* implement "touch" (empty sequence)
1067 * modify operation (note that there
1068 * is no symmetry with the UNIX command,
1069 * since \"touch\" on a non-existent entry
1071 printf( "warning: no attributes to %sadd (entry=\"%s\")\n",
1072 newentry ? "" : "change or ", dn );
1075 for ( i = 0; pmods[ i ] != NULL; ++i ) {
1076 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1077 if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
1079 _("%s: attribute \"%s\" has no values (entry=\"%s\")\n"),
1080 prog, pmods[i]->mod_type, dn );
1081 return LDAP_PARAM_ERROR;
1086 for ( i = 0; pmods[ i ] != NULL; ++i ) {
1087 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1089 op == LDAP_MOD_REPLACE ? _("replace") :
1090 op == LDAP_MOD_ADD ? _("add") :
1091 op == LDAP_MOD_INCREMENT ? _("increment") :
1092 op == LDAP_MOD_DELETE ? _("delete") :
1094 pmods[ i ]->mod_type );
1096 if ( pmods[ i ]->mod_bvalues != NULL ) {
1097 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1098 bvp = pmods[ i ]->mod_bvalues[ j ];
1100 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
1101 if ( !isascii( bvp->bv_val[ k ] )) {
1107 printf( _("\tNOT ASCII (%ld bytes)\n"), bvp->bv_len );
1109 printf( "\t%s\n", bvp->bv_val );
1118 printf( "%sadding new entry \"%s\"\n", dont ? "!" : "", dn );
1120 printf( "%smodifying entry \"%s\"\n", dont ? "!" : "", dn );
1126 rc = ldap_add_ext( ld, dn, pmods, pctrls, NULL, &msgid );
1128 rc = ldap_modify_ext( ld, dn, pmods, pctrls, NULL, &msgid );
1131 if ( rc != LDAP_SUCCESS ) {
1132 /* print error message about failed update including DN */
1133 fprintf( stderr, _("%s: update failed: %s\n"), prog, dn );
1134 tool_perror( newentry ? "ldap_add" : "ldap_modify",
1135 rc, NULL, NULL, NULL, NULL );
1137 } else if ( verbose ) {
1138 printf( _("modify complete\n") );
1141 rc = process_response( ld, msgid,
1142 newentry ? LDAP_RES_ADD : LDAP_RES_MODIFY, dn );
1157 LDAPControl **pctrls )
1162 printf( _("%sdeleting entry \"%s\"\n"), dont ? "!" : "", dn );
1164 rc = ldap_delete_ext( ld, dn, pctrls, NULL, &msgid );
1165 if ( rc != LDAP_SUCCESS ) {
1166 fprintf( stderr, _("%s: delete failed: %s\n"), prog, dn );
1167 tool_perror( "ldap_delete", rc, NULL, NULL, NULL, NULL );
1169 } else if ( verbose ) {
1170 printf( _("delete complete") );
1173 rc = process_response( ld, msgid, LDAP_RES_DELETE, dn );
1191 LDAPControl **pctrls )
1196 printf( _("%smodifying rdn of entry \"%s\"\n"), dont ? "!" : "", dn );
1198 printf( _("\tnew RDN: \"%s\" (%skeep existing values)\n"),
1199 newrdn, deleteoldrdn ? _("do not ") : "" );
1202 rc = ldap_rename( ld, dn, newrdn, newsup, deleteoldrdn,
1203 pctrls, NULL, &msgid );
1204 if ( rc != LDAP_SUCCESS ) {
1205 fprintf( stderr, _("%s: rename failed: %s\n"), prog, dn );
1206 tool_perror( "ldap_rename", rc, NULL, NULL, NULL, NULL );
1209 printf( _("rename completed\n") );
1212 rc = process_response( ld, msgid, LDAP_RES_RENAME, dn );
1224 res2str( int res ) {
1228 case LDAP_RES_DELETE:
1229 return "ldap_delete";
1230 case LDAP_RES_MODIFY:
1231 return "ldap_modify";
1232 case LDAP_RES_MODRDN:
1233 return "ldap_rename";
1238 return "ldap_unknown";
1241 static int process_response(
1248 int rc = LDAP_OTHER, msgtype;
1249 struct timeval tv = { 0, 0 };
1251 char *text = NULL, *matched = NULL, **refs = NULL;
1252 LDAPControl **ctrls = NULL;
1256 tv.tv_usec = 100000;
1258 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
1259 if ( tool_check_abandon( ld, msgid ) ) {
1260 return LDAP_CANCELLED;
1264 ldap_get_option( ld, LDAP_OPT_RESULT_CODE, &rc );
1273 msgtype = ldap_msgtype( res );
1275 rc = ldap_parse_result( ld, res, &err, &matched, &text, &refs, &ctrls, 1 );
1276 if ( rc == LDAP_SUCCESS ) rc = err;
1279 if ( rc == LDAP_X_TXN_SPECIFY_OKAY ) {
1283 if ( rc != LDAP_SUCCESS ) {
1284 tool_perror( res2str( op ), rc, NULL, matched, text, refs );
1285 } else if ( msgtype != op ) {
1286 fprintf( stderr, "%s: msgtype: expected %d got %d\n",
1287 res2str( op ), op, msgtype );
1291 if ( text ) ldap_memfree( text );
1292 if ( matched ) ldap_memfree( matched );
1293 if ( text ) ber_memvfree( (void **)refs );
1295 if ( ctrls != NULL ) {
1296 tool_print_ctrls( ld, ctrls );
1297 ldap_controls_free( ctrls );