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;
77 static int process_ldif_rec LDAP_P(( char *rbuf, int lineno ));
78 static int domodify LDAP_P((
79 const struct berval *dn,
83 static int dodelete LDAP_P((
84 const struct berval *dn,
85 LDAPControl **pctrls ));
86 static int dorename LDAP_P((
87 const struct berval *dn,
88 const struct berval *newrdn,
89 const struct berval *newsup,
91 LDAPControl **pctrls ));
92 static int process_response(
96 const struct berval *dn );
100 static int txnabort = 0;
101 struct berval *txn_id = NULL;
107 fprintf( stderr, _("Add or modify entries from an LDAP server\n\n"));
108 fprintf( stderr, _("usage: %s [options]\n"), prog);
109 fprintf( stderr, _(" The list of desired operations are read from stdin"
110 " or from the file\n"));
111 fprintf( stderr, _(" specified by \"-f file\".\n"));
112 fprintf( stderr, _("Add or modify options:\n"));
113 fprintf( stderr, _(" -a add values (%s)\n"),
114 (ldapadd ? _("default") : _("default is to replace")));
115 fprintf( stderr, _(" -c continuous operation mode (do not stop on errors)\n"));
116 fprintf( stderr, _(" -E [!]ext=extparam modify extensions"
117 " (! indicate s criticality)\n"));
118 fprintf( stderr, _(" -f file read operations from `file'\n"));
119 fprintf( stderr, _(" -M enable Manage DSA IT control (-MM to make critical)\n"));
120 fprintf( stderr, _(" -P version protocol version (default: 3)\n"));
123 _(" [!]txn=<commit|abort> (transaction)\n"));
125 fprintf( stderr, _(" -S file write skipped modifications to `file'\n"));
128 exit( EXIT_FAILURE );
132 const char options[] = "aE:rS:"
133 "cd:D:e:f:h:H:IMnNO:o:p:P:QR:U:vVw:WxX:y:Y:Z";
136 handle_private_option( int i )
138 char *control, *cvalue;
142 case 'E': /* modify extensions */
143 if( protocol == LDAP_VERSION2 ) {
144 fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
146 exit( EXIT_FAILURE );
149 /* should be extended to support comma separated list of
150 * [!]key[=value] parameters, e.g. -E !foo,bar=567
155 if( optarg[0] == '!' ) {
160 control = ber_strdup( optarg );
161 if ( (cvalue = strchr( control, '=' )) != NULL ) {
166 if( strcasecmp( control, "txn" ) == 0 ) {
170 _("txn control previously specified\n"));
171 exit( EXIT_FAILURE );
173 if( cvalue != NULL ) {
174 if( strcasecmp( cvalue, "abort" ) == 0 ) {
176 } else if( strcasecmp( cvalue, "commit" ) != 0 ) {
177 fprintf( stderr, _("Invalid value for txn control, %s\n"),
179 exit( EXIT_FAILURE );
187 fprintf( stderr, _("Invalid modify extension name: %s\n"),
197 case 'r': /* replace (obsolete) */
200 case 'S': /* skipped modifications to file */
201 if( rejfile != NULL ) {
202 fprintf( stderr, _("%s: -S previously specified\n"), prog );
203 exit( EXIT_FAILURE );
205 rejfile = ber_strdup( optarg );
216 main( int argc, char **argv )
218 char *rbuf = NULL, *rejbuf = NULL;
220 struct LDIFFP *ldiffp, ldifdummy = {0};
221 char *matched_msg, *error_msg;
222 int rc, retval, ldifrc;
225 int lineno, nextline = 0, lmax = 0;
228 prog = lutil_progname( "ldapmodify", argc, argv );
230 /* strncmp instead of strcmp since NT binaries carry .exe extension */
231 ldapadd = ( strncasecmp( prog, "ldapadd", sizeof("ldapadd")-1 ) == 0 );
233 tool_init( ldapadd ? TOOL_ADD : TOOL_MODIFY );
235 tool_args( argc, argv );
237 if ( argc != optind ) usage();
239 if ( rejfile != NULL ) {
240 if (( rejfp = fopen( rejfile, "w" )) == NULL ) {
242 retval = EXIT_FAILURE;
249 if ( infile != NULL ) {
250 if (( ldiffp = ldif_open( infile, "r" )) == NULL ) {
252 retval = EXIT_FAILURE;
256 ldifdummy.fp = stdin;
260 if ( debug ) ldif_debug = debug;
262 ld = tool_conn_setup( dont, 0 );
265 if ( pw_file || want_bindpw ) {
267 rc = lutil_get_filed_password( pw_file, &passwd );
269 retval = EXIT_FAILURE;
273 passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
274 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
282 /* start transaction */
283 rc = ldap_txn_start_s( ld, NULL, NULL, &txn_id );
284 if( rc != LDAP_SUCCESS ) {
285 tool_perror( "ldap_txn_start_s", rc, NULL, NULL, NULL, NULL );
287 retval = EXIT_FAILURE;
303 c[i].ldctl_oid = LDAP_CONTROL_X_TXN_SPEC;
304 c[i].ldctl_value = *txn_id;
305 c[i].ldctl_iscritical = 1;
311 tool_server_controls( ld, c, i );
316 while (( rc == 0 || contoper ) && ( ldifrc = ldif_read_record( ldiffp, &nextline,
317 &rbuf, &lmax )) > 0 )
320 len = strlen( rbuf );
321 if (( rejbuf = (char *)ber_memalloc( len+1 )) == NULL ) {
323 retval = EXIT_FAILURE;
326 memcpy( rejbuf, rbuf, len+1 );
329 rc = process_ldif_rec( rbuf, lineno );
332 if ( rc ) retval = rc;
334 fprintf(rejfp, _("# Error: %s (%d)"), ldap_err2string(rc), rc);
337 ldap_get_option(ld, LDAP_OPT_MATCHED_DN, &matched_msg);
338 if ( matched_msg != NULL ) {
339 if ( *matched_msg != '\0' ) {
340 fprintf( rejfp, _(", matched DN: %s"), matched_msg );
342 ldap_memfree( matched_msg );
346 ldap_get_option(ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, &error_msg);
347 if ( error_msg != NULL ) {
348 if ( *error_msg != '\0' ) {
349 fprintf( rejfp, _(", additional info: %s"), error_msg );
351 ldap_memfree( error_msg );
353 fprintf( rejfp, "\n%s\n", rejbuf );
356 if (rejfp) ber_memfree( rejbuf );
364 if( retval == 0 && txn ) {
365 rc = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
366 if ( rc != LDAP_OPT_SUCCESS ) {
367 fprintf( stderr, "Could not unset controls for ldap_txn_end\n");
370 /* create transaction */
371 rc = ldap_txn_end_s( ld, !txnabort, txn_id, NULL, NULL, NULL );
372 if( rc != LDAP_SUCCESS ) {
373 tool_perror( "ldap_txn_end_s", rc, NULL, NULL, NULL, NULL );
379 if ( !dont && ld != NULL ) {
386 if ( rejfp != NULL ) {
390 if ( ldiffp != NULL && ldiffp != &ldifdummy ) {
391 ldif_close( ldiffp );
399 process_ldif_rec( char *rbuf, int linenum )
402 int lrflags = ldapadd ? LDIF_DEFAULT_ADD : 0;
404 struct berval rbuf_bv;
407 if ( getenv( "LDIF_ENTRIES_ONLY" ) ) {
408 lrflags |= LDIF_ENTRIES_ONLY;
410 if ( getenv( "LDIF_NO_CONTROLS" ) ) {
411 lrflags |= LDIF_NO_CONTROLS;
413 #endif /* TEST_LDIF_API */
415 rbuf_bv.bv_val = rbuf;
416 rbuf_bv.bv_len = 0; /* not used */
417 rc = ldap_parse_ldif_record( &rbuf_bv, linenum, &lr, prog, lrflags );
419 /* If default controls are set (as with -M option) and controls are
420 specified in the LDIF file, we must add the default controls to
421 the list of controls sent with the ldap operation.
425 LDAPControl **defctrls = NULL; /* Default server controls */
426 LDAPControl **newctrls = NULL;
427 ldap_get_option(ld, LDAP_OPT_SERVER_CONTROLS, &defctrls);
429 int npc=0; /* Num of LDIF controls */
430 int ndefc=0; /* Num of default controls */
431 while (lr.lr_ctrls[npc]) npc++; /* Count LDIF controls */
432 while (defctrls[ndefc]) ndefc++; /* Count default controls */
433 newctrls = ber_memrealloc(lr.lr_ctrls,
434 (npc+ndefc+1)*sizeof(LDAPControl*));
436 if (newctrls == NULL) {
440 lr.lr_ctrls = newctrls;
441 for (i=npc; i<npc+ndefc; i++) {
442 lr.lr_ctrls[i] = ldap_control_dup(defctrls[i-npc]);
443 if (lr.lr_ctrls[i] == NULL) {
448 lr.lr_ctrls[npc+ndefc] = NULL;
450 ldap_controls_free(defctrls); /* Must be freed by library */
456 if ( LDAP_REQ_DELETE == lr.lr_op ) {
457 rc = dodelete( &lr.lr_dn, lr.lr_ctrls );
458 } else if ( LDAP_REQ_RENAME == lr.lr_op ) {
459 rc = dorename( &lr.lr_dn, &lr.lr_newrdn, &lr.lr_newsuperior, lr.lr_deleteoldrdn, lr.lr_ctrls );
460 } else if ( ( LDAP_REQ_ADD == lr.lr_op ) || ( LDAP_REQ_MODIFY == lr.lr_op ) ) {
461 rc = domodify( &lr.lr_dn, lr.lr_mods, lr.lr_ctrls, LDAP_REQ_ADD == lr.lr_op );
463 /* record skipped e.g. version: or comment or something we don't handle yet */
466 if ( rc == LDAP_SUCCESS ) {
471 ldap_ldif_record_done( &lr );
479 const struct berval *dn,
481 LDAPControl **pctrls,
484 int rc, i, j, k, notascii, op;
487 if ( ( dn == NULL ) || ( dn->bv_val == NULL ) ) {
488 fprintf( stderr, _("%s: no DN specified\n"), prog );
489 return( LDAP_PARAM_ERROR );
492 if ( pmods == NULL ) {
493 /* implement "touch" (empty sequence)
494 * modify operation (note that there
495 * is no symmetry with the UNIX command,
496 * since \"touch\" on a non-existent entry
498 printf( "warning: no attributes to %sadd (entry=\"%s\")\n",
499 newentry ? "" : "change or ", dn->bv_val );
502 for ( i = 0; pmods[ i ] != NULL; ++i ) {
503 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
504 if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
506 _("%s: attribute \"%s\" has no values (entry=\"%s\")\n"),
507 prog, pmods[i]->mod_type, dn->bv_val );
508 return LDAP_PARAM_ERROR;
513 for ( i = 0; pmods[ i ] != NULL; ++i ) {
514 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
516 op == LDAP_MOD_REPLACE ? _("replace") :
517 op == LDAP_MOD_ADD ? _("add") :
518 op == LDAP_MOD_INCREMENT ? _("increment") :
519 op == LDAP_MOD_DELETE ? _("delete") :
521 pmods[ i ]->mod_type );
523 if ( pmods[ i ]->mod_bvalues != NULL ) {
524 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
525 bvp = pmods[ i ]->mod_bvalues[ j ];
527 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
528 if ( !isascii( bvp->bv_val[ k ] )) {
534 printf( _("\tNOT ASCII (%ld bytes)\n"), bvp->bv_len );
536 printf( "\t%s\n", bvp->bv_val );
545 printf( "%sadding new entry \"%s\"\n", dont ? "!" : "", dn->bv_val );
547 printf( "%smodifying entry \"%s\"\n", dont ? "!" : "", dn->bv_val );
553 rc = ldap_add_ext( ld, dn->bv_val, pmods, pctrls, NULL, &msgid );
555 rc = ldap_modify_ext( ld, dn->bv_val, pmods, pctrls, NULL, &msgid );
558 if ( rc != LDAP_SUCCESS ) {
559 /* print error message about failed update including DN */
560 fprintf( stderr, _("%s: update failed: %s\n"), prog, dn->bv_val );
561 tool_perror( newentry ? "ldap_add" : "ldap_modify",
562 rc, NULL, NULL, NULL, NULL );
565 rc = process_response( ld, msgid,
566 newentry ? LDAP_RES_ADD : LDAP_RES_MODIFY, dn );
568 if ( verbose && rc == LDAP_SUCCESS ) {
569 printf( _("modify complete\n") );
584 const struct berval *dn,
585 LDAPControl **pctrls )
590 assert( dn != NULL );
591 assert( dn->bv_val != NULL );
592 printf( _("%sdeleting entry \"%s\"\n"), dont ? "!" : "", dn->bv_val );
594 rc = ldap_delete_ext( ld, dn->bv_val, pctrls, NULL, &msgid );
595 if ( rc != LDAP_SUCCESS ) {
596 fprintf( stderr, _("%s: delete failed: %s\n"), prog, dn->bv_val );
597 tool_perror( "ldap_delete", rc, NULL, NULL, NULL, NULL );
600 rc = process_response( ld, msgid, LDAP_RES_DELETE, dn );
602 if ( verbose && rc == LDAP_SUCCESS ) {
603 printf( _("delete complete\n") );
617 const struct berval *dn,
618 const struct berval *newrdn,
619 const struct berval *newsup,
621 LDAPControl **pctrls )
626 assert( dn != NULL );
627 assert( dn->bv_val != NULL );
628 assert( newrdn != NULL );
629 assert( newrdn->bv_val != NULL );
630 printf( _("%smodifying rdn of entry \"%s\"\n"), dont ? "!" : "", dn->bv_val );
632 printf( _("\tnew RDN: \"%s\" (%skeep existing values)\n"),
633 newrdn->bv_val, deleteoldrdn ? _("do not ") : "" );
636 rc = ldap_rename( ld, dn->bv_val, newrdn->bv_val,
637 ( newsup && newsup->bv_val ) ? newsup->bv_val : NULL,
638 deleteoldrdn, pctrls, NULL, &msgid );
639 if ( rc != LDAP_SUCCESS ) {
640 fprintf( stderr, _("%s: rename failed: %s\n"), prog, dn->bv_val );
641 tool_perror( "ldap_rename", rc, NULL, NULL, NULL, NULL );
644 rc = process_response( ld, msgid, LDAP_RES_RENAME, dn );
646 if ( verbose && rc == LDAP_SUCCESS ) {
647 printf( _("rename complete\n") );
663 case LDAP_RES_DELETE:
664 return "ldap_delete";
665 case LDAP_RES_MODIFY:
666 return "ldap_modify";
667 case LDAP_RES_MODRDN:
668 return "ldap_rename";
673 return "ldap_unknown";
676 static int process_response(
680 const struct berval *dn )
683 int rc = LDAP_OTHER, msgtype;
684 struct timeval tv = { 0, 0 };
686 char *text = NULL, *matched = NULL, **refs = NULL;
687 LDAPControl **ctrls = NULL;
689 assert( dn != NULL );
694 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
695 if ( tool_check_abandon( ld, msgid ) ) {
696 return LDAP_CANCELLED;
700 ldap_get_option( ld, LDAP_OPT_RESULT_CODE, &rc );
701 tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
710 msgtype = ldap_msgtype( res );
712 rc = ldap_parse_result( ld, res, &err, &matched, &text, &refs, &ctrls, 1 );
713 if ( rc == LDAP_SUCCESS ) rc = err;
716 if ( rc == LDAP_X_TXN_SPECIFY_OKAY ) {
720 if ( rc != LDAP_SUCCESS ) {
721 tool_perror( res2str( op ), rc, NULL, matched, text, refs );
722 } else if ( msgtype != op ) {
723 fprintf( stderr, "%s: msgtype: expected %d got %d\n",
724 res2str( op ), op, msgtype );
728 if ( text ) ldap_memfree( text );
729 if ( matched ) ldap_memfree( matched );
730 if ( text ) ber_memvfree( (void **)refs );
733 tool_print_ctrls( ld, ctrls );
734 ldap_controls_free( ctrls );