#include <stdlib.h>
#include <ac/ctype.h>
+#include <ac/signal.h>
#include <ac/string.h>
#include <ac/unistd.h>
#include <ldap.h>
#include <ldif.h>
-static LDAP *ld = NULL;
static char *prog;
static char *binddn = NULL;
static char *passwd = NULL;
static char *ldaphost = NULL;
-static int ldapport = LDAP_PORT;
+static int ldapport = 0;
static int new, replace, not, verbose, contoper, force, valsfromfiles;
+static LDAP *ld;
#define safe_realloc( ptr, size ) ( ptr == NULL ? malloc( size ) : \
realloc( ptr, size ))
#define T_NEWRDNSTR "newrdn"
#define T_DELETEOLDRDNSTR "deleteoldrdn"
-static void
-usage(char *s)
-{
- fprintf(stderr, "Usage: %s [options] [dn]...\n", s);
- fprintf(stderr, " -a\t\tadd new entries\n");
- fprintf(stderr, " -b\t\tread in binary\n");
- fprintf(stderr, " -c\t\tcontinuous operation mode\n");
- fprintf(stderr, " -D bindnd\tbind dn\n");
- fprintf(stderr, " -d level\tdebugging level\n");
- fprintf(stderr, " -F\t\tforce changes regardless of input\n");
- fprintf(stderr, " -f file\tread from file\n");
- fprintf(stderr, " -h host\tldap sever\n");
-#ifdef HAVE_KERBEROS
- fprintf(stderr, " -K\t\tuse Kerberos step 1\n");
- fprintf(stderr, " -k\t\tuse Kerberos instead of Simple Password authentication\n");
-#endif
- fprintf(stderr, " -n\t\tmake no modifications\n");
- fprintf(stderr, " -p port\tldap port\n");
- fprintf(stderr, " -r\t\tremove old RDN\n");
- fprintf(stderr, " -v\t\tverbose\n");
- fprintf(stderr, " -W\t\tprompt for bind password\n");
- fprintf(stderr, " -w passwd\tbind password (for simple authentication)\n");
- exit(1);
-}
static int process_ldapmod_rec LDAP_P(( char *rbuf ));
static int process_ldif_rec LDAP_P(( char *rbuf ));
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)
+main( int argc, char **argv )
{
- char *infile = NULL;
- char *rbuf, *start, *p, *q;
- FILE *fp = NULL;
- int rc, i, use_ldif, want_passwd;
- int authmethod = LDAP_AUTH_SIMPLE;
-
- if ((prog = strrchr(argv[0], '/')) == NULL)
- prog = argv[0];
- else
- prog++;
+ char *infile, *rbuf, *start, *p, *q;
+ FILE *fp;
+ 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";
- new = (strcmp(prog, "ldapadd") == 0);
+ if (( prog = strrchr( argv[ 0 ], '/' )) == NULL ) {
+ prog = argv[ 0 ];
+ } else {
+ ++prog;
+ }
+ new = ( strcmp( prog, "ldapadd" ) == 0 );
- not = verbose = valsfromfiles = want_passwd = 0;
+ infile = NULL;
+ not = verbose = valsfromfiles = want_bindpw = debug = 0;
+ authmethod = LDAP_AUTH_SIMPLE;
+ version = -1;
- while ((i = getopt(argc, argv, "abcD:d:Ff:h:Kknp:rtvWw:")) != EOF)
- {
- switch(i)
- {
+ while (( i = getopt( argc, argv, "WFabckKnrtvh:p:D:w:d:f:" )) != EOF ) {
+ switch( i ) {
case 'a': /* add */
new = 1;
break;
-
- case 'b': /* read values from files (for binary attributes) */
+ case 'b': /* read values from files (for binary attributes) */
valsfromfiles = 1;
break;
-
- case 'c': /* continuous operation */
+ case 'c': /* continuous operation */
contoper = 1;
break;
-
- case 'D': /* bind DN */
- binddn = 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 */
- break;
-
- case 'F': /* force all changes records to be used */
- force = 1;
- break;
-
- case 'f': /* read from file */
- infile = strdup( optarg );
- break;
-
- case 'h': /* ldap host */
- ldaphost = strdup( optarg );
+ case 'r': /* default is to replace rather than add values */
+ replace = 1;
break;
-
- case 'k': /* kerberos bind */
+ case 'k': /* kerberos bind */
#ifdef HAVE_KERBEROS
- authmethod = LDAP_AUTH_KRBV4;
+ authmethod = LDAP_AUTH_KRBV4;
#else
- fprintf(stderr, "%s was not compiled with Kerberos support\n", argv[0]);
+ fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
#endif
break;
-
- case 'K': /* kerberos bind, part 1 only */
+ case 'K': /* kerberos bind, part 1 only */
#ifdef HAVE_KERBEROS
- authmethod = LDAP_AUTH_KRBV41;
+ authmethod = LDAP_AUTH_KRBV41;
#else
- fprintf(stderr, "%s was not compiled with Kerberos support\n", argv[0]);
+ fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
#endif
break;
-
- case 'n': /* print adds, don't actually do them */
- ++not;
+ case 'F': /* force all changes records to be used */
+ force = 1;
break;
-
- case 'p':
+ case 'h': /* ldap host */
+ ldaphost = strdup( optarg );
+ break;
+ case 'D': /* bind DN */
+ binddn = strdup( optarg );
+ break;
+ case 'w': /* password */
+ passwd = strdup( optarg );
+ break;
+ case 'd':
+ debug |= atoi( optarg );
+ break;
+ case 'f': /* read from file */
+ infile = strdup( optarg );
+ break;
+ case 'p':
ldapport = atoi( optarg );
break;
-
- case 'r': /* default is to replace rather than add values */
- replace = 1;
+ case 'n': /* print adds, don't actually do them */
+ ++not;
break;
-
- case 'v': /* verbose mode */
+ case 'v': /* verbose mode */
verbose++;
break;
-
- case 'W':
- want_passwd++;
- break;
-
- case 'w': /* password */
- passwd = strdup( optarg );
- break;
-
- default:
- usage(prog);
+ 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 );
}
}
- if (argc - optind != 0)
- usage(prog);
-
- if (want_passwd && !passwd)
- passwd = strdup(getpass("Enter LDAP password: "));
+ if ( argc - optind != 0 ) {
+ fprintf( stderr, usage, prog );
+ exit( 1 );
+ }
if ( infile != NULL ) {
if (( fp = fopen( infile, "r" )) == NULL ) {
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 );
}
/* this seems prudent */
- ldap_set_option( ld, LDAP_OPT_DEREF, LDAP_DEREF_NEVER);
+ {
+ int deref = LDAP_DEREF_NEVER;
+ ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
+ }
+
+ 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" );
if ( !use_ldif && ( q = strchr( rbuf, '\n' )) != NULL ) {
for ( p = rbuf; p < q; ++p ) {
- if ( !isdigit( *p )) {
+ if ( !isdigit( (unsigned char) *p )) {
break;
}
}
new_entry = new;
- modop = rc = got_all = saw_replica = delete_entry = expect_modop = 0;
+ rc = got_all = saw_replica = delete_entry = expect_modop = 0;
expect_deleteoldrdn = expect_newrdn = expect_sep = expect_ct = 0;
linenum = 0;
deleteoldrdn = 1;
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;
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' ) {