/* ldapmodify.c - generic program to modify or add entries using LDAP */
-#define DISABLE_BRIDGE
#include "portable.h"
#include <stdio.h>
-#include <ac/string.h>
#include <stdlib.h>
-#include <ctype.h>
-#include <sys/types.h>
+
+#include <ac/ctype.h>
+#include <ac/signal.h>
+#include <ac/string.h>
+#include <ac/unistd.h>
+
#include <sys/stat.h>
+
+#ifdef HAVE_SYS_FILE_H
#include <sys/file.h>
+#endif
+#ifdef HAVE_FCNTL_H
#include <fcntl.h>
-#ifndef VMS
-#include <unistd.h>
-#endif /* VMS */
+#endif
#include <lber.h>
#include <ldap.h>
#include <ldif.h>
-#include "ldapconfig.h"
-
static char *prog;
-static char *binddn = LDAPMODIFY_BINDDN;
-static char *passwd = LDAPMODIFY_BIND_CRED;
-static char *ldaphost = LDAPHOST;
-static int ldapport = LDAP_PORT;
+static char *binddn = NULL;
+static char *passwd = NULL;
+static char *ldaphost = NULL;
+static int ldapport = 0;
static int new, replace, not, verbose, contoper, force, valsfromfiles;
static LDAP *ld;
-#ifdef LDAP_DEBUG
-extern int ldap_debug, lber_debug;
-#endif /* LDAP_DEBUG */
-
#define safe_realloc( ptr, size ) ( ptr == NULL ? malloc( size ) : \
realloc( ptr, size ))
#define T_DELETEOLDRDNSTR "deleteoldrdn"
-#ifdef NEEDPROTOS
-static int process_ldapmod_rec( char *rbuf );
-static int process_ldif_rec( char *rbuf );
-static void addmodifyop( LDAPMod ***pmodsp, int modop, char *attr,
- char *value, int vlen );
-static int domodify( char *dn, LDAPMod **pmods, int newentry );
-static int dodelete( char *dn );
-static int domodrdn( char *dn, char *newrdn, int deleteoldrdn );
-static void freepmods( LDAPMod **pmods );
-static int fromfile( char *path, struct berval *bv );
-static char *read_one_record( FILE *fp );
-#else /* NEEDPROTOS */
-static int process_ldapmod_rec();
-static int process_ldif_rec();
-static void addmodifyop();
-static int domodify();
-static int dodelete();
-static int domodrdn();
-static void freepmods();
-static int fromfile();
-static char *read_one_record();
-#endif /* NEEDPROTOS */
-
-
-main( argc, argv )
- int argc;
- char **argv;
+static int process_ldapmod_rec LDAP_P(( char *rbuf ));
+static int process_ldif_rec LDAP_P(( char *rbuf ));
+static void addmodifyop LDAP_P(( LDAPMod ***pmodsp, int modop, char *attr,
+ char *value, int vlen ));
+static int domodify LDAP_P(( char *dn, LDAPMod **pmods, int newentry ));
+static int dodelete LDAP_P(( char *dn ));
+static int domodrdn LDAP_P(( char *dn, char *newrdn, int deleteoldrdn ));
+static void freepmods LDAP_P(( LDAPMod **pmods ));
+static int fromfile LDAP_P(( char *path, struct berval *bv ));
+static char *read_one_record LDAP_P(( FILE *fp ));
+
+
+int
+main( int argc, char **argv )
{
char *infile, *rbuf, *start, *p, *q;
FILE *fp;
- int rc, i, kerberos, use_ldif, authmethod;
- char *usage = "usage: %s [-abcknrvF] [-d debug-level] [-h ldaphost] [-p ldapport] [-D binddn] [-w passwd] [ -f file | < entryfile ]\n";
-
- extern char *optarg;
- extern int optind;
+ int rc, i, use_ldif, authmethod, version, want_bindpw, debug;
+ char *usage = "usage: %s [-abcknrvWF] [-d debug-level] [-h ldaphost] [-P version] [-p ldapport] [-D binddn] [-w passwd] [ -f file | < entryfile ]\n";
if (( prog = strrchr( argv[ 0 ], '/' )) == NULL ) {
prog = argv[ 0 ];
new = ( strcmp( prog, "ldapadd" ) == 0 );
infile = NULL;
- kerberos = not = verbose = valsfromfiles = 0;
+ not = verbose = valsfromfiles = want_bindpw = debug = 0;
+ authmethod = LDAP_AUTH_SIMPLE;
+ version = -1;
- while (( i = getopt( argc, argv, "FabckKnrtvh:p:D:w:d:f:" )) != EOF ) {
+ while (( i = getopt( argc, argv, "WFabckKnrtvh:p:D:w:d:f:" )) != EOF ) {
switch( i ) {
case 'a': /* add */
new = 1;
replace = 1;
break;
case 'k': /* kerberos bind */
- kerberos = 2;
+#ifdef HAVE_KERBEROS
+ authmethod = LDAP_AUTH_KRBV4;
+#else
+ fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
+#endif
break;
case 'K': /* kerberos bind, part 1 only */
- kerberos = 1;
+#ifdef HAVE_KERBEROS
+ authmethod = LDAP_AUTH_KRBV41;
+#else
+ fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
+#endif
break;
case 'F': /* force all changes records to be used */
force = 1;
passwd = strdup( optarg );
break;
case 'd':
-#ifdef LDAP_DEBUG
- ldap_debug = lber_debug = atoi( optarg ); /* */
-#else /* LDAP_DEBUG */
- fprintf( stderr, "%s: compile with -DLDAP_DEBUG for debugging\n",
- prog );
-#endif /* LDAP_DEBUG */
+ debug |= atoi( optarg );
break;
case 'f': /* read from file */
infile = strdup( optarg );
case 'v': /* verbose mode */
verbose++;
break;
+ case 'W':
+ want_bindpw++;
+ break;
+ case 'P':
+ switch(optarg[0])
+ {
+ case '2':
+ version = LDAP_VERSION2;
+ break;
+ case '3':
+ version = LDAP_VERSION3;
+ break;
+ }
+ break;
default:
fprintf( stderr, usage, prog );
exit( 1 );
fp = stdin;
}
+ if ( debug ) {
+ lber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug );
+ ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug );
+ ldif_debug = debug;
+ }
+
+#ifdef SIGPIPE
+ (void) SIGNAL( SIGPIPE, SIG_IGN );
+#endif
if ( !not ) {
- if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
- perror( "ldap_open" );
+ if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
+ perror( "ldap_init" );
exit( 1 );
}
- ld->ld_deref = LDAP_DEREF_NEVER; /* this seems prudent */
+ /* this seems prudent */
+ {
+ int deref = LDAP_DEREF_NEVER;
+ ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
+ }
- if ( !kerberos ) {
- authmethod = LDAP_AUTH_SIMPLE;
- } else if ( kerberos == 1 ) {
- authmethod = LDAP_AUTH_KRBV41;
- } else {
- authmethod = LDAP_AUTH_KRBV4;
+ if (want_bindpw)
+ passwd = getpass("Enter LDAP Password: ");
+
+ if( version != -1 ) {
+ ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
}
+
if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
ldap_perror( ld, "ldap_bind" );
exit( 1 );
if ( !use_ldif && ( q = strchr( rbuf, '\n' )) != NULL ) {
for ( p = rbuf; p < q; ++p ) {
- if ( !isdigit( *p )) {
+ if ( !isdigit( (unsigned char) *p )) {
break;
}
}
}
exit( rc );
+
+ /* UNREACHABLE */
+ return(0);
}
pmods = NULL;
dn = newrdn = NULL;
- while ( rc == 0 && ( line = str_getline( &rbuf )) != NULL ) {
+ while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
++linenum;
if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
expect_sep = 0;
continue;
}
- if ( str_parse_line( line, &type, &value, &vlen ) < 0 ) {
+ if ( ldif_parse_line( line, &type, &value, &vlen ) < 0 ) {
fprintf( stderr, "%s: invalid format (line %d of entry: %s\n",
prog, linenum, dn == NULL ? "" : dn );
rc = LDAP_PARAM_ERROR;
if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
++saw_replica;
if (( p = strchr( value, ':' )) == NULL ) {
- replicaport = LDAP_PORT;
+ replicaport = 0;
} else {
*p++ = '\0';
replicaport = atoi( p );
rbuf = NULL;
} else {
if ( *(p-1) == '\\' ) { /* lines ending in '\' are continued */
- strcpy( p - 1, p );
+ SAFEMEMCPY( p - 1, p, strlen( p ) + 1 );
rbuf = p;
continue;
}
value = p;
}
- for ( attr = line; *attr != '\0' && isspace( *attr ); ++attr ) {
+ for ( attr = line;
+ *attr != '\0' && isspace( (unsigned char) *attr ); ++attr ) {
; /* skip attribute leading white space */
}
- for ( q = p - 1; q > attr && isspace( *q ); --q ) {
+ for ( q = p - 1; q > attr && isspace( (unsigned char) *q ); --q ) {
*q = '\0'; /* remove attribute trailing white space */
}
if ( value != NULL ) {
- while ( isspace( *value )) {
+ while ( isspace( (unsigned char) *value )) {
++value; /* skip value leading white space */
}
for ( q = value + strlen( value ) - 1; q > value &&
- isspace( *q ); --q ) {
+ isspace( (unsigned char) *q ); --q ) {
*q = '\0'; /* remove value trailing white space */
}
if ( *value == '\0' ) {
for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
bvp = pmods[ i ]->mod_bvalues[ j ];
notascii = 0;
- for ( k = 0; k < bvp->bv_len; ++k ) {
+ for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
if ( !isascii( bvp->bv_val[ k ] )) {
notascii = 1;
break;
eof = feof( fp );
fclose( fp );
- if ( rlen != bv->bv_len ) {
+ if ( (unsigned long) rlen != bv->bv_len ) {
perror( path );
free( bv->bv_val );
return( -1 );