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-2006 The OpenLDAP Foundation.
6 * Portions Copyright 1998-2003 Kurt D. Zeilenga.
7 * Portions Copyright 1998-2001 Net Boolean Incorporated.
8 * Portions Copyright 2001-2003 IBM Corporation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted only as authorized by the OpenLDAP
15 * A copy of this license is available in the file LICENSE in the
16 * top-level directory of the distribution or, alternatively, at
17 * <http://www.OpenLDAP.org/license.html>.
19 /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
20 * All rights reserved.
22 * Redistribution and use in source and binary forms are permitted
23 * provided that this notice is preserved and that due credit is given
24 * to the University of Michigan at Ann Arbor. The name of the
25 * University may not be used to endorse or promote products derived
26 * from this software without specific prior written permission. This
27 * software is provided ``as is'' without express or implied warranty.
30 * This work was originally developed by the University of Michigan
31 * (as part of U-MICH LDAP). Additional significant contributors
41 #include <ac/stdlib.h>
43 #include <ac/string.h>
44 #include <ac/unistd.h>
45 #include <ac/socket.h>
48 #ifdef HAVE_SYS_STAT_H
52 #ifdef HAVE_SYS_FILE_H
62 #include "lutil_ldap.h"
64 #include "ldap_defaults.h"
71 static int ldapadd, force = 0;
72 static char *rejfile = NULL;
73 static LDAP *ld = NULL;
75 #define LDAPMOD_MAXLINE 4096
77 /* strings found in replog/LDIF entries (mostly lifted from slurpd/slurp.h) */
78 #define T_VERSION_STR "version"
79 #define T_REPLICA_STR "replica"
81 #define T_CONTROL_STR "control"
82 #define T_CHANGETYPESTR "changetype"
83 #define T_ADDCTSTR "add"
84 #define T_MODIFYCTSTR "modify"
85 #define T_DELETECTSTR "delete"
86 #define T_MODRDNCTSTR "modrdn"
87 #define T_MODDNCTSTR "moddn"
88 #define T_RENAMECTSTR "rename"
89 #define T_MODOPADDSTR "add"
90 #define T_MODOPREPLACESTR "replace"
91 #define T_MODOPDELETESTR "delete"
92 #define T_MODOPINCREMENTSTR "increment"
93 #define T_MODSEPSTR "-"
94 #define T_NEWRDNSTR "newrdn"
95 #define T_DELETEOLDRDNSTR "deleteoldrdn"
96 #define T_NEWSUPSTR "newsuperior"
99 static int process_ldif_rec LDAP_P(( char *rbuf, int count ));
100 static int parse_ldif_control LDAP_P(( char *line, LDAPControl ***pctrls ));
101 static void addmodifyop LDAP_P((
102 LDAPMod ***pmodsp, int modop,
104 struct berval *value ));
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(
124 static char *read_one_record LDAP_P(( FILE *fp ));
128 static int txnabort = 0;
129 struct berval *txn_id = NULL;
135 fprintf( stderr, _("Add or modify entries from an LDAP server\n\n"));
136 fprintf( stderr, _("usage: %s [options]\n"), prog);
137 fprintf( stderr, _(" The list of desired operations are read from stdin"
138 " or from the file\n"));
139 fprintf( stderr, _(" specified by \"-f file\".\n"));
140 fprintf( stderr, _("Add or modify options:\n"));
141 fprintf( stderr, _(" -a add values (%s)\n"),
142 (ldapadd ? _("default") : _("default is to replace")));
143 fprintf( stderr, _(" -E [!]ext=extparam modify extensions"
144 " (! indicate s criticality)\n"));
147 _(" [!]txn=<commit|abort> (transaction)\n"));
149 fprintf( stderr, _(" -F force all changes records to be used\n"));
150 fprintf( stderr, _(" -S file write skipped modifications to `file'\n"));
153 exit( EXIT_FAILURE );
157 const char options[] = "aE:FrS:"
158 "cd:D:e:f:h:H:IkKMnO: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 'F': /* force all changes records to be used */
226 case 'r': /* replace (obsolete) */
229 case 'S': /* skipped modifications to file */
230 if( rejfile != NULL ) {
231 fprintf( stderr, _("%s: -S previously specified\n"), prog );
232 exit( EXIT_FAILURE );
234 rejfile = ber_strdup( optarg );
245 main( int argc, char **argv )
247 char *rbuf, *start, *rejbuf = NULL;
249 char *matched_msg, *error_msg;
255 prog = lutil_progname( "ldapmodify", argc, argv );
257 /* strncmp instead of strcmp since NT binaries carry .exe extension */
258 ldapadd = ( strncasecmp( prog, "ldapadd", sizeof("ldapadd")-1 ) == 0 );
260 tool_init( ldapadd ? TOOL_ADD : TOOL_MODIFY );
262 tool_args( argc, argv );
264 if ( argc != optind ) usage();
266 if ( rejfile != NULL ) {
267 if (( rejfp = fopen( rejfile, "w" )) == NULL ) {
269 return( EXIT_FAILURE );
275 if ( infile != NULL ) {
276 if (( fp = fopen( infile, "r" )) == NULL ) {
278 return( EXIT_FAILURE );
284 if ( debug ) ldif_debug = debug;
286 ld = tool_conn_setup( dont, 0 );
289 if ( pw_file || want_bindpw ) {
291 rc = lutil_get_filed_password( pw_file, &passwd );
292 if( rc ) return EXIT_FAILURE;
294 passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
295 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
303 /* start transaction */
304 rc = ldap_txn_start_s( ld, NULL, NULL, &txn_id );
305 if( rc != LDAP_SUCCESS ) {
306 tool_perror( "ldap_txn_start_s", rc, NULL, NULL, NULL, NULL );
307 if( txn > 1 ) return EXIT_FAILURE;
321 c[i].ldctl_oid = LDAP_CONTROL_X_TXN_SPEC;
322 c[i].ldctl_value = *txn_id;
323 c[i].ldctl_iscritical = 1;
329 tool_server_controls( ld, c, i );
334 while (( rc == 0 || contoper ) &&
335 ( rbuf = read_one_record( fp )) != NULL )
342 len = strlen( rbuf );
343 if (( rejbuf = (char *)ber_memalloc( len+1 )) == NULL ) {
345 exit( EXIT_FAILURE );
347 memcpy( rejbuf, rbuf, len+1 );
350 rc = process_ldif_rec( start, count );
352 if ( rc ) retval = rc;
354 fprintf(rejfp, _("# Error: %s (%d)"), ldap_err2string(rc), rc);
357 ldap_get_option(ld, LDAP_OPT_MATCHED_DN, &matched_msg);
358 if ( matched_msg != NULL ) {
359 if ( *matched_msg != '\0' ) {
360 fprintf( rejfp, _(", matched DN: %s"), matched_msg );
362 ldap_memfree( matched_msg );
366 ldap_get_option(ld, LDAP_OPT_ERROR_STRING, &error_msg);
367 if ( error_msg != NULL ) {
368 if ( *error_msg != '\0' ) {
369 fprintf( rejfp, _(", additional info: %s"), error_msg );
371 ldap_memfree( error_msg );
373 fprintf( rejfp, "\n%s\n", rejbuf );
376 if (rejfp) free( rejbuf );
381 if( retval == 0 && txn ) {
382 rc = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
383 if ( rc != LDAP_OPT_SUCCESS ) {
384 fprintf( stderr, "Could not unset controls for ldap_txn_end\n");
387 /* create transaction */
388 rc = ldap_txn_end_s( ld, !txnabort, txn_id, NULL, NULL, NULL );
389 if( rc != LDAP_SUCCESS ) {
390 tool_perror( "ldap_txn_end_s", rc, NULL, NULL, NULL, NULL );
400 if ( rejfp != NULL ) {
410 process_ldif_rec( char *rbuf, int count )
412 char *line, *dn, *type, *newrdn, *newsup, *p;
413 int rc, linenum, modop, replicaport;
414 int expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
415 int expect_deleteoldrdn, deleteoldrdn;
416 int saw_replica, use_record, new_entry, delete_entry, got_all;
420 LDAPControl **pctrls;
424 rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
425 expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
426 expect_sep = expect_ct = 0;
433 dn = newrdn = newsup = NULL;
435 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
438 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
444 if ( ldif_parse_line( line, &type, &val.bv_val, &val.bv_len ) < 0 ) {
445 fprintf( stderr, _("%s: invalid format (line %d) entry: \"%s\"\n"),
446 prog, linenum, dn == NULL ? "" : dn );
447 rc = LDAP_PARAM_ERROR;
452 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
454 if (( p = strchr( val.bv_val, ':' )) == NULL ) {
458 if ( lutil_atoi( &replicaport, p ) != 0 ) {
459 fprintf( stderr, _("%s: unable to parse replica port \"%s\" (line %d) entry: \"%s\"\n"),
460 prog, p, linenum, dn == NULL ? "" : dn );
461 rc = LDAP_PARAM_ERROR;
465 if ( ldaphost != NULL &&
466 strcasecmp( val.bv_val, ldaphost ) == 0 &&
467 replicaport == ldapport )
471 } else if ( count == 1 && linenum == 1 &&
472 strcasecmp( type, T_VERSION_STR ) == 0 )
475 if( val.bv_len == 0 || lutil_atoi( &v, val.bv_val) != 0 || v != 1 ) {
477 _("%s: invalid version %s, line %d (ignored)\n"),
478 prog, val.bv_val, linenum );
482 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
483 if (( dn = ber_strdup( val.bv_val )) == NULL ) {
485 exit( EXIT_FAILURE );
489 goto end_line; /* skip all lines until we see "dn:" */
493 /* Check for "control" tag after dn and before changetype. */
494 if (strcasecmp(type, T_CONTROL_STR) == 0) {
495 /* Parse and add it to the list of controls */
496 rc = parse_ldif_control( line, &pctrls );
499 _("%s: Error processing %s line, line %d: %s\n"),
500 prog, T_CONTROL_STR, linenum, ldap_err2string(rc) );
506 if ( !use_record && saw_replica ) {
507 printf(_("%s: skipping change record for entry: %s\n"),
509 printf(_("\t(LDAP host/port does not match replica: lines)\n"));
512 ber_memfree( val.bv_val );
516 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
517 #ifdef LIBERAL_CHANGETYPE_MODOP
518 /* trim trailing spaces (and log warning ...) */
520 for ( icnt = val.bv_len; --icnt > 0; ) {
521 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
526 if ( ++icnt != val.bv_len ) {
527 fprintf( stderr, _("%s: illegal trailing space after"
528 " \"%s: %s\" trimmed (line %d of entry \"%s\")\n"),
529 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
530 val.bv_val[icnt] = '\0';
532 #endif /* LIBERAL_CHANGETYPE_MODOP */
534 if ( strcasecmp( val.bv_val, T_MODIFYCTSTR ) == 0 ) {
537 } else if ( strcasecmp( val.bv_val, T_ADDCTSTR ) == 0 ) {
539 } else if ( strcasecmp( val.bv_val, T_MODRDNCTSTR ) == 0
540 || strcasecmp( val.bv_val, T_MODDNCTSTR ) == 0
541 || strcasecmp( val.bv_val, T_RENAMECTSTR ) == 0)
544 } else if ( strcasecmp( val.bv_val, T_DELETECTSTR ) == 0 ) {
545 got_all = delete_entry = 1;
548 _("%s: unknown %s \"%s\" (line %d of entry \"%s\")\n"),
549 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
550 rc = LDAP_PARAM_ERROR;
553 } else if ( ldapadd ) { /* missing changetype => add */
555 modop = LDAP_MOD_ADD;
557 expect_modop = 1; /* missing changetype => modify */
561 if ( expect_modop ) {
562 #ifdef LIBERAL_CHANGETYPE_MODOP
563 /* trim trailing spaces (and log warning ...) */
565 for ( icnt = val.bv_len; --icnt > 0; ) {
566 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) break;
569 if ( ++icnt != val.bv_len ) {
570 fprintf( stderr, _("%s: illegal trailing space after"
571 " \"%s: %s\" trimmed (line %d of entry \"%s\")\n"),
572 prog, type, val.bv_val, linenum, dn );
573 val.bv_val[icnt] = '\0';
575 #endif /* LIBERAL_CHANGETYPE_MODOP */
579 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
580 modop = LDAP_MOD_ADD;
582 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
583 modop = LDAP_MOD_REPLACE;
584 addmodifyop( &pmods, modop, val.bv_val, NULL );
586 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
587 modop = LDAP_MOD_DELETE;
588 addmodifyop( &pmods, modop, val.bv_val, NULL );
590 } else if ( strcasecmp( type, T_MODOPINCREMENTSTR ) == 0 ) {
591 modop = LDAP_MOD_INCREMENT;
592 addmodifyop( &pmods, modop, val.bv_val, NULL );
594 } else { /* no modify op: use default */
595 modop = ldapadd ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
599 if ( expect_newrdn ) {
600 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
601 if (( newrdn = ber_strdup( val.bv_val )) == NULL ) {
603 exit( EXIT_FAILURE );
605 expect_deleteoldrdn = 1;
608 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
609 " \"%s:\" (line %d of entry \"%s\")\n"),
610 prog, T_NEWRDNSTR, type, linenum, dn );
611 rc = LDAP_PARAM_ERROR;
613 } else if ( expect_deleteoldrdn ) {
614 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
615 deleteoldrdn = ( *val.bv_val == '0' ) ? 0 : 1;
616 expect_deleteoldrdn = 0;
620 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
621 " \"%s:\" (line %d of entry \"%s\")\n"),
622 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
623 rc = LDAP_PARAM_ERROR;
625 } else if ( expect_newsup ) {
626 if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
627 if (( newsup = ber_strdup( val.bv_val )) == NULL ) {
629 exit( EXIT_FAILURE );
633 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
634 " \"%s:\" (line %d of entry \"%s\")\n"),
635 prog, T_NEWSUPSTR, type, linenum, dn );
636 rc = LDAP_PARAM_ERROR;
638 } else if ( got_all ) {
640 _("%s: extra lines at end (line %d of entry \"%s\")\n"),
642 rc = LDAP_PARAM_ERROR;
644 if ( new_entry && strcasecmp( type, T_DN_STR ) == 0 ) {
645 fprintf( stderr, _("%s: attributeDescription \"%s\":"
646 " (possible missing newline"
647 " after line %d of entry \"%s\"?)\n"),
648 prog, type, linenum - 1, dn );
650 addmodifyop( &pmods, modop, type, &val );
655 ber_memfree( val.bv_val );
662 if( version && linenum == 1 ) {
666 /* If default controls are set (as with -M option) and controls are
667 specified in the LDIF file, we must add the default controls to
668 the list of controls sent with the ldap operation.
672 LDAPControl **defctrls = NULL; /* Default server controls */
673 LDAPControl **newctrls = NULL;
674 ldap_get_option(ld, LDAP_OPT_SERVER_CONTROLS, &defctrls);
676 int npc=0; /* Num of LDIF controls */
677 int ndefc=0; /* Num of default controls */
678 while (pctrls[npc]) npc++; /* Count LDIF controls */
679 while (defctrls[ndefc]) ndefc++; /* Count default controls */
680 newctrls = ber_memrealloc(pctrls,
681 (npc+ndefc+1)*sizeof(LDAPControl*));
683 if (newctrls == NULL) {
688 for (i=npc; i<npc+ndefc; i++) {
689 pctrls[i] = ldap_control_dup(defctrls[i-npc]);
690 if (pctrls[i] == NULL) {
695 pctrls[npc+ndefc] = NULL;
697 ldap_controls_free(defctrls); /* Must be freed by library */
704 if ( delete_entry ) {
705 rc = dodelete( dn, pctrls );
706 } else if ( newrdn != NULL ) {
707 rc = dorename( dn, newrdn, newsup, deleteoldrdn, pctrls );
709 rc = domodify( dn, pmods, pctrls, new_entry );
712 if ( rc == LDAP_SUCCESS ) {
720 if ( newrdn != NULL ) {
723 if ( newsup != NULL ) {
726 if ( pmods != NULL ) {
727 ldap_mods_free( pmods, 1 );
729 if (pctrls != NULL) {
730 ldap_controls_free( pctrls );
736 /* Parse an LDIF control line of the form
737 control: oid [true/false] [: value] or
738 control: oid [true/false] [:: base64-value] or
739 control: oid [true/false] [:< url]
740 The control is added to the list of controls in *ppctrls.
745 LDAPControl ***ppctrls )
748 int criticality = 0; /* Default is false if not present */
751 ber_len_t value_len = 0;
753 char *s, *oidStart, *pcolon;
754 LDAPControl *newctrl = NULL;
755 LDAPControl **pctrls = NULL;
757 if (ppctrls) pctrls = *ppctrls;
758 s = line + strlen(T_CONTROL_STR); /* Skip over "control" */
759 pcolon = s; /* Save this position for later */
760 if (*s++ != ':') { /* Make sure colon follows */
761 return ( LDAP_PARAM_ERROR );
763 while (*s && isspace((unsigned char)*s)) {
764 s++; /* Skip white space before OID */
767 /* OID should come next. Validate and extract it. */
768 if (*s == 0) return ( LDAP_PARAM_ERROR );
770 while (isdigit((unsigned char)*s) || *s == '.') {
771 s++; /* OID should be digits or . */
774 return ( LDAP_PARAM_ERROR ); /* OID was not present */
776 if (*s) { /* End of OID should be space or NULL */
777 if (!isspace((unsigned char)*s)) {
778 return ( LDAP_PARAM_ERROR ); /* else OID contained invalid chars */
780 *s++ = 0; /* Replace space with null to terminate */
783 oid = ber_strdup(oidStart);
784 if (oid == NULL) return ( LDAP_NO_MEMORY );
786 /* Optional Criticality field is next. */
787 while (*s && isspace((unsigned char)*s)) {
788 s++; /* Skip white space before criticality */
790 if (strncasecmp(s, "true", 4) == 0) {
794 else if (strncasecmp(s, "false", 5) == 0) {
799 /* Optional value field is next */
800 while (*s && isspace((unsigned char)*s)) {
801 s++; /* Skip white space before value */
804 if (*s != ':') { /* If value is present, must start with : */
805 rc = LDAP_PARAM_ERROR;
809 /* Shift value down over OID and criticality so it's in the form
811 control:: base64-value
813 Then we can use ldif_parse_line to extract and decode the value
815 while ( (*pcolon++ = *s++) != 0) { /* Shift value */
818 rc = ldif_parse_line(line, &type, &val, &value_len);
819 if (type) ber_memfree(type); /* Don't need this field*/
821 rc = LDAP_PARAM_ERROR;
826 /* Create a new LDAPControl structure. */
827 newctrl = (LDAPControl *)ber_memalloc(sizeof(LDAPControl));
828 if ( newctrl == NULL ) {
832 newctrl->ldctl_oid = oid;
834 newctrl->ldctl_iscritical = criticality;
835 newctrl->ldctl_value.bv_len = value_len;
836 newctrl->ldctl_value.bv_val = val;
839 /* Add the new control to the passed-in list of controls. */
842 while ( pctrls[i] ) { /* Count the # of controls passed in */
846 /* Allocate 1 more slot for the new control and 1 for the NULL. */
847 pctrls = (LDAPControl **) ber_memrealloc(pctrls,
848 (i+2)*(sizeof(LDAPControl *)));
849 if (pctrls == NULL) {
860 if (newctrl->ldctl_oid) ber_memfree(newctrl->ldctl_oid);
861 if (newctrl->ldctl_value.bv_val) {
862 ber_memfree(newctrl->ldctl_value.bv_val);
864 ber_memfree(newctrl);
866 if (val) ber_memfree(val);
867 if (oid) ber_memfree(oid);
884 modop |= LDAP_MOD_BVALUES;
887 if ( pmods != NULL ) {
888 for ( ; pmods[ i ] != NULL; ++i ) {
889 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
890 pmods[ i ]->mod_op == modop )
897 if ( pmods == NULL || pmods[ i ] == NULL ) {
898 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
899 sizeof( LDAPMod * ))) == NULL )
902 exit( EXIT_FAILURE );
906 pmods[ i + 1 ] = NULL;
908 pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod ));
909 if ( pmods[ i ] == NULL ) {
911 exit( EXIT_FAILURE );
914 pmods[ i ]->mod_op = modop;
915 pmods[ i ]->mod_type = ber_strdup( attr );
916 if ( pmods[ i ]->mod_type == NULL ) {
918 exit( EXIT_FAILURE );
924 if ( pmods[ i ]->mod_bvalues != NULL ) {
925 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
930 pmods[ i ]->mod_bvalues = (struct berval **) ber_memrealloc(
931 pmods[ i ]->mod_bvalues, (j + 2) * sizeof( struct berval * ));
932 if ( pmods[ i ]->mod_bvalues == NULL ) {
933 perror( "ber_realloc" );
934 exit( EXIT_FAILURE );
937 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
938 pmods[ i ]->mod_bvalues[ j ] = ber_bvdup( val );
939 if ( pmods[ i ]->mod_bvalues[ j ] == NULL ) {
940 perror( "ber_bvdup" );
941 exit( EXIT_FAILURE );
951 LDAPControl **pctrls,
954 int rc, i, j, k, notascii, op;
958 fprintf( stderr, _("%s: no DN specified\n"), prog );
959 return( LDAP_PARAM_ERROR );
962 if ( pmods == NULL ) {
963 /* implement "touch" (empty sequence)
964 * modify operation (note that there
965 * is no symmetry with the UNIX command,
966 * since \"touch\" on a non-existent entry
968 printf( "warning: no attributes to %sadd (entry=\"%s\")\n",
969 newentry ? "" : "change or ", dn );
972 for ( i = 0; pmods[ i ] != NULL; ++i ) {
973 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
974 if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
976 _("%s: attribute \"%s\" has no values (entry=\"%s\")\n"),
977 prog, pmods[i]->mod_type, dn );
978 return LDAP_PARAM_ERROR;
983 for ( i = 0; pmods[ i ] != NULL; ++i ) {
984 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
986 op == LDAP_MOD_REPLACE ? _("replace") :
987 op == LDAP_MOD_ADD ? _("add") :
988 op == LDAP_MOD_INCREMENT ? _("increment") :
989 op == LDAP_MOD_DELETE ? _("delete") :
991 pmods[ i ]->mod_type );
993 if ( pmods[ i ]->mod_bvalues != NULL ) {
994 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
995 bvp = pmods[ i ]->mod_bvalues[ j ];
997 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
998 if ( !isascii( bvp->bv_val[ k ] )) {
1004 printf( _("\tNOT ASCII (%ld bytes)\n"), bvp->bv_len );
1006 printf( "\t%s\n", bvp->bv_val );
1015 printf( "%sadding new entry \"%s\"\n", dont ? "!" : "", dn );
1017 printf( "%smodifying entry \"%s\"\n", dont ? "!" : "", dn );
1023 rc = ldap_add_ext( ld, dn, pmods, pctrls, NULL, &msgid );
1025 rc = ldap_modify_ext( ld, dn, pmods, pctrls, NULL, &msgid );
1028 if ( rc != LDAP_SUCCESS ) {
1029 /* print error message about failed update including DN */
1030 fprintf( stderr, _("%s: update failed: %s\n"), prog, dn );
1031 tool_perror( newentry ? "ldap_add" : "ldap_modify",
1032 rc, NULL, NULL, NULL, NULL );
1034 } else if ( verbose ) {
1035 printf( _("modify complete\n") );
1038 rc = process_response( ld, msgid,
1039 newentry ? LDAP_RES_ADD : LDAP_RES_MODIFY, dn );
1054 LDAPControl **pctrls )
1059 printf( _("%sdeleting entry \"%s\"\n"), dont ? "!" : "", dn );
1061 rc = ldap_delete_ext( ld, dn, pctrls, NULL, &msgid );
1062 if ( rc != LDAP_SUCCESS ) {
1063 fprintf( stderr, _("%s: delete failed: %s\n"), prog, dn );
1064 tool_perror( "ldap_delete", rc, NULL, NULL, NULL, NULL );
1066 } else if ( verbose ) {
1067 printf( _("delete complete") );
1070 rc = process_response( ld, msgid, LDAP_RES_DELETE, dn );
1088 LDAPControl **pctrls )
1093 printf( _("%smodifying rdn of entry \"%s\"\n"), dont ? "!" : "", dn );
1095 printf( _("\tnew RDN: \"%s\" (%skeep existing values)\n"),
1096 newrdn, deleteoldrdn ? _("do not ") : "" );
1099 rc = ldap_rename( ld, dn, newrdn, newsup, deleteoldrdn,
1100 pctrls, NULL, &msgid );
1101 if ( rc != LDAP_SUCCESS ) {
1102 fprintf( stderr, _("%s: rename failed: %s\n"), prog, dn );
1103 tool_perror( "ldap_rename", rc, NULL, NULL, NULL, NULL );
1106 printf( _("rename completed\n") );
1109 rc = process_response( ld, msgid, LDAP_RES_RENAME, dn );
1121 res2str( int res ) {
1125 case LDAP_RES_DELETE:
1126 return "ldap_delete";
1127 case LDAP_RES_MODIFY:
1128 return "ldap_modify";
1129 case LDAP_RES_MODRDN:
1130 return "ldap_rename";
1135 return "ldap_unknown";
1138 static int process_response(
1145 int rc = LDAP_OTHER, msgtype;
1146 struct timeval tv = { 0, 0 };
1148 char *text = NULL, *matched = NULL, **refs = NULL;
1149 LDAPControl **ctrls = NULL;
1153 tv.tv_usec = 100000;
1155 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
1156 if ( tool_check_abandon( ld, msgid ) ) {
1157 return LDAP_CANCELLED;
1161 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
1170 msgtype = ldap_msgtype( res );
1172 rc = ldap_parse_result( ld, res, &err, &matched, &text, &refs, &ctrls, 1 );
1173 if ( rc == LDAP_SUCCESS ) rc = err;
1176 if ( rc == LDAP_X_TXN_SPECIFY_OKAY ) {
1180 if ( rc != LDAP_SUCCESS ) {
1181 tool_perror( res2str( op ), rc, NULL, matched, text, refs );
1182 } else if ( msgtype != op ) {
1183 fprintf( stderr, "%s: msgtype: expected %d got %d\n",
1184 res2str( op ), op, msgtype );
1188 if ( text ) ldap_memfree( text );
1189 if ( matched ) ldap_memfree( matched );
1190 if ( text ) ber_memvfree( (void **)refs );
1192 if ( ctrls != NULL ) {
1193 tool_print_ctrls( ld, ctrls );
1194 ldap_controls_free( ctrls );
1201 read_one_record( FILE *fp )
1203 char *buf, line[ LDAPMOD_MAXLINE ];
1209 while ( fgets( line, sizeof(line), fp ) != NULL ) {
1210 int len = strlen( line );
1212 if( len < 2 || ( len == 2 && *line == '\r' )) {
1220 if ( lcur + len + 1 > lmax ) {
1221 lmax = LDAPMOD_MAXLINE
1222 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
1224 if (( buf = (char *)ber_memrealloc( buf, lmax )) == NULL ) {
1225 perror( "realloc" );
1226 exit( EXIT_FAILURE );
1230 strcpy( buf + lcur, line );