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) 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;
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 );
438 return LDAP_NO_MEMORY;
440 vals = btype+lines+1;
441 freeval = (char *)(vals+lines+1);
444 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
447 if ( *line == '\n' || *line == '\0' ) {
453 if ( line[0] == '-' && !line[1] ) {
454 BER_BVZERO( btype+i );
459 if ( ( rc = ldif_parse_line2( line, btype+i, vals+i, &freev ) ) < 0 ) {
460 fprintf( stderr, _("%s: invalid format (line %d) entry: \"%s\"\n"),
461 prog, linenum+i, dn == NULL ? "" : dn );
462 rc = LDAP_PARAM_ERROR;
468 if ( !use_record && !BVICMP( btype+i, &BV_REPLICA )) {
471 if (( p = strchr( vals[i].bv_val, ':' )) == NULL ) {
475 if ( lutil_atoi( &replicaport, p ) != 0 ) {
476 fprintf( stderr, _("%s: unable to parse replica port \"%s\" (line %d) entry: \"%s\"\n"),
477 prog, p, linenum+i, dn == NULL ? "" : dn );
478 rc = LDAP_PARAM_ERROR;
482 if ( ldaphost != NULL &&
483 strcasecmp( vals[i].bv_val, ldaphost ) == 0 &&
484 replicaport == ldapport )
488 } else if ( linenum+i == 1 && !BVICMP( btype+i, &BV_VERSION )) {
490 if( vals[i].bv_len == 0 || lutil_atoi( &v, vals[i].bv_val) != 0 || v != 1 ) {
492 _("%s: invalid version %s, line %d (ignored)\n"),
493 prog, vals[i].bv_val, linenum );
497 } else if ( !BVICMP( btype+i, &BV_DN )) {
500 if ( !use_record && saw_replica ) {
501 printf(_("%s: skipping change record for entry: %s at line %d\n"),
502 prog, dn, linenum+i);
503 printf(_("\t(LDAP host/port does not match replica: lines)\n"));
508 /* skip all lines until we see "dn:" */
512 /* check to make sure there was a dn: line */
525 if( version && lines == 1 ) {
531 /* Check for "control" tag after dn and before changetype. */
532 if (!BVICMP( btype+i, &BV_CONTROL)) {
533 /* Parse and add it to the list of controls */
534 rc = parse_ldif_control( vals+i, &pctrls );
537 _("%s: Error processing %s line, line %d: %s\n"),
538 prog, BV_CONTROL.bv_val, linenum+i, ldap_err2string(rc) );
544 _("%s: Expecting more input after %s line, line %d\n"),
545 prog, btype[i-1].bv_val, linenum+i );
547 rc = LDAP_PARAM_ERROR;
552 /* Check for changetype */
553 if ( !BVICMP( btype+i, &BV_CHANGETYPE )) {
554 #ifdef LIBERAL_CHANGETYPE_MODOP
555 /* trim trailing spaces (and log warning ...) */
557 for ( icnt = vals[i].bv_len; --icnt > 0; ) {
558 if ( !isspace( (unsigned char) vals[i].bv_val[icnt] ) ) {
563 if ( ++icnt != vals[i].bv_len ) {
564 fprintf( stderr, _("%s: illegal trailing space after"
565 " \"%s: %s\" trimmed (line %d, entry \"%s\")\n"),
566 prog, BV_CHANGETYPE.bv_val, vals[i].bv_val, linenum+i, dn );
567 vals[i].bv_val[icnt] = '\0';
569 #endif /* LIBERAL_CHANGETYPE_MODOP */
571 if ( BVICMP( vals+i, &BV_MODIFYCT ) == 0 ) {
574 } else if ( BVICMP( vals+i, &BV_ADDCT ) == 0 ) {
576 modop = LDAP_MOD_ADD;
577 } else if ( BVICMP( vals+i, &BV_MODRDNCT ) == 0
578 || BVICMP( vals+i, &BV_MODDNCT ) == 0
579 || BVICMP( vals+i, &BV_RENAMECT ) == 0)
584 if ( BVICMP( btype+i, &BV_NEWRDN )) {
585 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
586 " \"%s:\" (line %d, entry \"%s\")\n"),
587 prog, BV_NEWRDN.bv_val, btype[i].bv_val, linenum+i, dn );
588 rc = LDAP_PARAM_ERROR;
591 newrdn = vals[i].bv_val;
595 if ( BVICMP( btype+i, &BV_DELETEOLDRDN )) {
596 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
597 " \"%s:\" (line %d, entry \"%s\")\n"),
598 prog, BV_DELETEOLDRDN.bv_val, btype[i].bv_val, linenum+i, dn );
599 rc = LDAP_PARAM_ERROR;
602 deleteoldrdn = ( vals[i].bv_val[0] == '0' ) ? 0 : 1;
605 if ( BVICMP( btype+i, &BV_NEWSUP )) {
606 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
607 " \"%s:\" (line %d, entry \"%s\")\n"),
608 prog, BV_NEWSUP.bv_val, btype[i].bv_val, linenum+i, dn );
609 rc = LDAP_PARAM_ERROR;
612 newsup = vals[i].bv_val;
616 } else if ( BVICMP( vals+i, &BV_DELETECT ) == 0 ) {
617 got_all = delete_entry = 1;
620 _("%s: unknown %s \"%s\" (line %d, entry \"%s\")\n"),
621 prog, BV_CHANGETYPE.bv_val, vals[i].bv_val, linenum+i, dn );
622 rc = LDAP_PARAM_ERROR;
626 } else if ( ldapadd ) { /* missing changetype => add */
628 modop = LDAP_MOD_ADD;
630 expect_modop = 1; /* missing changetype => modify */
636 _("%s: extra lines at end (line %d, entry \"%s\")\n"),
637 prog, linenum+i, dn );
638 rc = LDAP_PARAM_ERROR;
650 /* Make sure all attributes with multiple values are contiguous */
651 for (; i<lines; i++) {
652 for (j=i+1; j<lines; j++) {
653 if ( !BVICMP( btype+i, btype+j )) {
655 /* out of order, move intervening attributes down */
659 for (k=j; k>i; k--) {
660 btype[k] = btype[k-1];
662 freeval[k] = freeval[k-1];
673 /* Allocate space for array of mods, array of pointers to mods,
674 * and array of pointers to values, allowing for NULL terminators
675 * for the pointer arrays...
677 lm = ber_memalloc( nmods * sizeof(LDAPMod) +
678 (nmods+1) * sizeof(LDAPMod*) +
679 (lines + nmods - idn) * sizeof(struct berval *));
680 pmods = (LDAPMod **)(lm+nmods);
681 bvl = (struct berval **)(pmods+nmods+1);
686 for (i=idn; i<lines; i++) {
687 if ( !BVICMP( btype+i, &BV_DN )) {
688 fprintf( stderr, _("%s: attributeDescription \"%s\":"
689 " (possible missing newline"
690 " after line %d, entry \"%s\"?)\n"),
691 prog, btype[i].bv_val, linenum+i - 1, dn );
693 if ( BVICMP(btype+i,&bv)) {
696 lm[j].mod_op = LDAP_MOD_ADD | LDAP_MOD_BVALUES;
697 lm[j].mod_type = bv.bv_val;
698 lm[j].mod_bvalues = bvl+k;
709 mops = ber_memalloc( lines+1 );
713 for ( ; i<lines; i++ ) {
714 if ( expect_modop ) {
715 #ifdef LIBERAL_CHANGETYPE_MODOP
716 /* trim trailing spaces (and log warning ...) */
718 for ( icnt = vals[i].bv_len; --icnt > 0; ) {
719 if ( !isspace( (unsigned char) vals[i].bv_val[icnt] ) ) break;
722 if ( ++icnt != vals[i].bv_len ) {
723 fprintf( stderr, _("%s: illegal trailing space after"
724 " \"%s: %s\" trimmed (line %d, entry \"%s\")\n"),
725 prog, type, vals[i].bv_val, linenum+i, dn );
726 vals[i].bv_val[icnt] = '\0';
728 #endif /* LIBERAL_CHANGETYPE_MODOP */
732 if ( BVICMP( btype+i, &BV_MODOPADD ) == 0 ) {
733 modop = LDAP_MOD_ADD;
736 } else if ( BVICMP( btype+i, &BV_MODOPREPLACE ) == 0 ) {
737 /* defer handling these since they might have no values.
738 * Use the BVALUES flag to signal that these were
739 * deferred. If values are provided later, this
740 * flag will be switched off.
742 modop = LDAP_MOD_REPLACE;
743 mops[i] = modop | LDAP_MOD_BVALUES;
745 } else if ( BVICMP( btype+i, &BV_MODOPDELETE ) == 0 ) {
746 modop = LDAP_MOD_DELETE;
747 mops[i] = modop | LDAP_MOD_BVALUES;
749 } else if ( BVICMP( btype+i, &BV_MODOPINCREMENT ) == 0 ) {
750 modop = LDAP_MOD_INCREMENT;
753 } else { /* no modify op: invalid LDIF */
754 fprintf( stderr, _("%s: modify operation type is missing at"
755 " line %d, entry \"%s\"\n"),
756 prog, linenum+i, dn );
757 rc = LDAP_PARAM_ERROR;
761 } else if ( expect_sep && BER_BVISEMPTY( btype+i )) {
767 if ( BVICMP( btype+i, &bv )) {
768 fprintf( stderr, _("%s: wrong attributeType at"
769 " line %d, entry \"%s\"\n"),
770 prog, linenum+i, dn );
771 rc = LDAP_PARAM_ERROR;
775 /* If prev op was deferred and matches this type,
778 if ( (mops[i-1]&LDAP_MOD_BVALUES) && !BVICMP(btype+i,
786 #if 0 /* we should faithfully encode the LDIF, not combine */
787 /* Make sure all modops with multiple values are contiguous */
788 for (i=idn; i<lines; i++) {
789 if ( mops[i] == M_SEP )
791 for (j=i+1; j<lines; j++) {
792 if ( mops[j] == M_SEP || mops[i] != mops[j] )
794 if ( !BVICMP( btype+i, btype+j )) {
796 /* out of order, move intervening attributes down */
805 for (k=j; k>i; k--) {
806 btype[k] = btype[k-1];
808 freeval[k] = freeval[k-1];
823 /* Allocate space for array of mods, array of pointers to mods,
824 * and array of pointers to values, allowing for NULL terminators
825 * for the pointer arrays...
827 lm = ber_memalloc( nmods * sizeof(LDAPMod) +
828 (nmods+1) * sizeof(LDAPMod*) +
829 (lines + nmods - idn) * sizeof(struct berval *));
830 pmods = (LDAPMod **)(lm+nmods);
831 bvl = (struct berval **)(pmods+nmods+1);
837 for (i=idn; i<lines; i++) {
838 if ( mops[i] == M_SEP )
840 if ( mops[i] != mops[i-1] || BVICMP(btype+i,&bv)) {
843 lm[j].mod_op = mops[i] | LDAP_MOD_BVALUES;
844 lm[j].mod_type = bv.bv_val;
845 if ( mops[i] & LDAP_MOD_BVALUES ) {
846 lm[j].mod_bvalues = NULL;
848 lm[j].mod_bvalues = bvl+k;
859 /* If default controls are set (as with -M option) and controls are
860 specified in the LDIF file, we must add the default controls to
861 the list of controls sent with the ldap operation.
865 LDAPControl **defctrls = NULL; /* Default server controls */
866 LDAPControl **newctrls = NULL;
867 ldap_get_option(ld, LDAP_OPT_SERVER_CONTROLS, &defctrls);
869 int npc=0; /* Num of LDIF controls */
870 int ndefc=0; /* Num of default controls */
871 while (pctrls[npc]) npc++; /* Count LDIF controls */
872 while (defctrls[ndefc]) ndefc++; /* Count default controls */
873 newctrls = ber_memrealloc(pctrls,
874 (npc+ndefc+1)*sizeof(LDAPControl*));
876 if (newctrls == NULL) {
881 for (i=npc; i<npc+ndefc; i++) {
882 pctrls[i] = ldap_control_dup(defctrls[i-npc]);
883 if (pctrls[i] == NULL) {
888 pctrls[npc+ndefc] = NULL;
890 ldap_controls_free(defctrls); /* Must be freed by library */
896 if ( delete_entry ) {
897 rc = dodelete( dn, pctrls );
898 } else if ( newrdn != NULL ) {
899 rc = dorename( dn, newrdn, newsup, deleteoldrdn, pctrls );
901 rc = domodify( dn, pmods, pctrls, new_entry );
904 if ( rc == LDAP_SUCCESS ) {
910 if (pctrls != NULL) {
911 ldap_controls_free( pctrls );
916 if ( mops != NULL ) {
919 for (i=lines-1; i>=0; i--)
920 if ( freeval[i] ) ber_memfree( vals[i].bv_val );
921 ber_memfree( btype );
926 /* Parse an LDIF control line of the form
927 control: oid [true/false] [: value] or
928 control: oid [true/false] [:: base64-value] or
929 control: oid [true/false] [:< url]
930 The control is added to the list of controls in *ppctrls.
935 LDAPControl ***ppctrls )
938 int criticality = 0; /* Default is false if not present */
941 LDAPControl *newctrl = NULL;
942 LDAPControl **pctrls = NULL;
943 struct berval type, bv;
946 if (ppctrls) pctrls = *ppctrls;
947 /* OID should come first. Validate and extract it. */
949 if (*s == 0) return ( LDAP_PARAM_ERROR );
951 while (isdigit((unsigned char)*s) || *s == '.') {
952 s++; /* OID should be digits or . */
955 return ( LDAP_PARAM_ERROR ); /* OID was not present */
957 if (*s) { /* End of OID should be space or NULL */
958 if (!isspace((unsigned char)*s)) {
959 return ( LDAP_PARAM_ERROR ); /* else OID contained invalid chars */
961 *s++ = 0; /* Replace space with null to terminate */
964 oid = ber_strdup(oidStart);
965 if (oid == NULL) return ( LDAP_NO_MEMORY );
967 /* Optional Criticality field is next. */
968 while (*s && isspace((unsigned char)*s)) {
969 s++; /* Skip white space before criticality */
971 if (strncasecmp(s, "true", 4) == 0) {
975 else if (strncasecmp(s, "false", 5) == 0) {
980 /* Optional value field is next */
981 while (*s && isspace((unsigned char)*s)) {
982 s++; /* Skip white space before value */
985 if (*s != ':') { /* If value is present, must start with : */
986 rc = LDAP_PARAM_ERROR;
990 /* Back up so value is in the form
994 Then we can use ldif_parse_line2 to extract and decode the value
999 rc = ldif_parse_line2(s, &type, &bv, &freeval);
1001 rc = LDAP_PARAM_ERROR;
1006 /* Create a new LDAPControl structure. */
1007 newctrl = (LDAPControl *)ber_memalloc(sizeof(LDAPControl));
1008 if ( newctrl == NULL ) {
1009 rc = LDAP_NO_MEMORY;
1012 newctrl->ldctl_oid = oid;
1014 newctrl->ldctl_iscritical = criticality;
1016 newctrl->ldctl_value = bv;
1018 ber_dupbv( &newctrl->ldctl_value, &bv );
1020 /* Add the new control to the passed-in list of controls. */
1023 while ( pctrls[i] ) { /* Count the # of controls passed in */
1027 /* Allocate 1 more slot for the new control and 1 for the NULL. */
1028 pctrls = (LDAPControl **) ber_memrealloc(pctrls,
1029 (i+2)*(sizeof(LDAPControl *)));
1030 if (pctrls == NULL) {
1031 rc = LDAP_NO_MEMORY;
1034 pctrls[i] = newctrl;
1041 if (newctrl->ldctl_oid) ber_memfree(newctrl->ldctl_oid);
1042 if (newctrl->ldctl_value.bv_val) {
1043 ber_memfree(newctrl->ldctl_value.bv_val);
1045 ber_memfree(newctrl);
1047 if (oid) ber_memfree(oid);
1057 LDAPControl **pctrls,
1060 int rc, i, j, k, notascii, op;
1064 fprintf( stderr, _("%s: no DN specified\n"), prog );
1065 return( LDAP_PARAM_ERROR );
1068 if ( pmods == NULL ) {
1069 /* implement "touch" (empty sequence)
1070 * modify operation (note that there
1071 * is no symmetry with the UNIX command,
1072 * since \"touch\" on a non-existent entry
1074 printf( "warning: no attributes to %sadd (entry=\"%s\")\n",
1075 newentry ? "" : "change or ", dn );
1078 for ( i = 0; pmods[ i ] != NULL; ++i ) {
1079 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1080 if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
1082 _("%s: attribute \"%s\" has no values (entry=\"%s\")\n"),
1083 prog, pmods[i]->mod_type, dn );
1084 return LDAP_PARAM_ERROR;
1089 for ( i = 0; pmods[ i ] != NULL; ++i ) {
1090 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1092 op == LDAP_MOD_REPLACE ? _("replace") :
1093 op == LDAP_MOD_ADD ? _("add") :
1094 op == LDAP_MOD_INCREMENT ? _("increment") :
1095 op == LDAP_MOD_DELETE ? _("delete") :
1097 pmods[ i ]->mod_type );
1099 if ( pmods[ i ]->mod_bvalues != NULL ) {
1100 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1101 bvp = pmods[ i ]->mod_bvalues[ j ];
1103 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
1104 if ( !isascii( bvp->bv_val[ k ] )) {
1110 printf( _("\tNOT ASCII (%ld bytes)\n"), bvp->bv_len );
1112 printf( "\t%s\n", bvp->bv_val );
1121 printf( "%sadding new entry \"%s\"\n", dont ? "!" : "", dn );
1123 printf( "%smodifying entry \"%s\"\n", dont ? "!" : "", dn );
1129 rc = ldap_add_ext( ld, dn, pmods, pctrls, NULL, &msgid );
1131 rc = ldap_modify_ext( ld, dn, pmods, pctrls, NULL, &msgid );
1134 if ( rc != LDAP_SUCCESS ) {
1135 /* print error message about failed update including DN */
1136 fprintf( stderr, _("%s: update failed: %s\n"), prog, dn );
1137 tool_perror( newentry ? "ldap_add" : "ldap_modify",
1138 rc, NULL, NULL, NULL, NULL );
1140 } else if ( verbose ) {
1141 printf( _("modify complete\n") );
1144 rc = process_response( ld, msgid,
1145 newentry ? LDAP_RES_ADD : LDAP_RES_MODIFY, dn );
1160 LDAPControl **pctrls )
1165 printf( _("%sdeleting entry \"%s\"\n"), dont ? "!" : "", dn );
1167 rc = ldap_delete_ext( ld, dn, pctrls, NULL, &msgid );
1168 if ( rc != LDAP_SUCCESS ) {
1169 fprintf( stderr, _("%s: delete failed: %s\n"), prog, dn );
1170 tool_perror( "ldap_delete", rc, NULL, NULL, NULL, NULL );
1172 } else if ( verbose ) {
1173 printf( _("delete complete") );
1176 rc = process_response( ld, msgid, LDAP_RES_DELETE, dn );
1194 LDAPControl **pctrls )
1199 printf( _("%smodifying rdn of entry \"%s\"\n"), dont ? "!" : "", dn );
1201 printf( _("\tnew RDN: \"%s\" (%skeep existing values)\n"),
1202 newrdn, deleteoldrdn ? _("do not ") : "" );
1205 rc = ldap_rename( ld, dn, newrdn, newsup, deleteoldrdn,
1206 pctrls, NULL, &msgid );
1207 if ( rc != LDAP_SUCCESS ) {
1208 fprintf( stderr, _("%s: rename failed: %s\n"), prog, dn );
1209 tool_perror( "ldap_rename", rc, NULL, NULL, NULL, NULL );
1212 printf( _("rename completed\n") );
1215 rc = process_response( ld, msgid, LDAP_RES_RENAME, dn );
1227 res2str( int res ) {
1231 case LDAP_RES_DELETE:
1232 return "ldap_delete";
1233 case LDAP_RES_MODIFY:
1234 return "ldap_modify";
1235 case LDAP_RES_MODRDN:
1236 return "ldap_rename";
1241 return "ldap_unknown";
1244 static int process_response(
1251 int rc = LDAP_OTHER, msgtype;
1252 struct timeval tv = { 0, 0 };
1254 char *text = NULL, *matched = NULL, **refs = NULL;
1255 LDAPControl **ctrls = NULL;
1259 tv.tv_usec = 100000;
1261 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
1262 if ( tool_check_abandon( ld, msgid ) ) {
1263 return LDAP_CANCELLED;
1267 ldap_get_option( ld, LDAP_OPT_RESULT_CODE, &rc );
1268 tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
1277 msgtype = ldap_msgtype( res );
1279 rc = ldap_parse_result( ld, res, &err, &matched, &text, &refs, &ctrls, 1 );
1280 if ( rc == LDAP_SUCCESS ) rc = err;
1283 if ( rc == LDAP_X_TXN_SPECIFY_OKAY ) {
1287 if ( rc != LDAP_SUCCESS ) {
1288 tool_perror( res2str( op ), rc, NULL, matched, text, refs );
1289 } else if ( msgtype != op ) {
1290 fprintf( stderr, "%s: msgtype: expected %d got %d\n",
1291 res2str( op ), op, msgtype );
1295 if ( text ) ldap_memfree( text );
1296 if ( matched ) ldap_memfree( matched );
1297 if ( text ) ber_memvfree( (void **)refs );
1299 if ( ctrls != NULL ) {
1300 tool_print_ctrls( ld, ctrls );
1301 ldap_controls_free( ctrls );