diff --git a/ldap2dns.c b/ldap2dns.c index 281a503..a8a099a 100644 --- a/ldap2dns.c +++ b/ldap2dns.c @@ -109,6 +109,7 @@ static struct unsigned short port[MAXHOSTS]; char password[128]; int usedhosts; + int useduris; int is_daemon; unsigned int update_iv; unsigned int output; @@ -167,7 +168,7 @@ static void print_usage(void) printf(" -o db\t\tGenerate a BIND compatible zone files\n"); printf(" -L [filename]\tPrint output in LDIF format for reimport\n"); printf(" -h host\tHostname of LDAP server, defaults to localhost\n"); - printf(" -p port\tPortnumber to connect to LDAP server, defaults to %d\n", LDAP_PORT); + printf(" -p port\tPort number to connect to LDAP server, defaults to %d\n", LDAP_PORT); printf(" -H hostURI\tURI (ldap://hostname or ldaps://hostname of LDAP server\n"); printf(" -u numsecs\tUpdate DNS data after numsecs. Defaults to %d. Daemon mode only\n\t\t", UPDATE_INTERVAL); printf("\n"); @@ -185,6 +186,7 @@ static void parse_hosts(char* buf) char value[128], rest[512]; options.usedhosts = 0; + options.useduris = 0; for (i = 0; i 0) { res = ldap_initialize(&ldap_con, options.urildap[i]); if (options.verbose&1 && res == LDAP_SUCCESS) { printf("ldap_initialization successful (%s)\n", options.urildap[i]); } else if ( res != LDAP_SUCCESS ) { - printf("ldap_initialization to %s failed %d\n", options.urildap[i], ldap_err2string(res)); + fprintf(stderr, "ldap_initialization to %s failed %d\n", options.urildap[i], ldap_err2string(res)); ldap_con = NULL; return res; } version = LDAP_VERSION3; if ( (res = ldap_set_option(ldap_con, LDAP_OPT_PROTOCOL_VERSION, &version)) != LDAP_SUCCESS ) { - printf("ldap_set_option to %s failed with err %s!\n", options.urildap[i], ldap_err2string(res)); + fprintf(stderr, "ldap_set_option to %s failed with err %s!\n", options.urildap[i], ldap_err2string(res)); ldap_con = NULL; return res; } if ( options.use_tls[i] && (res = ldap_start_tls_s( ldap_con, NULL, NULL )) != LDAP_SUCCESS ) { - printf("ldap_start_tls_s to %s failed with err %s!\n", options.urildap[i], ldap_err2string(res)); + fprintf(stderr, "ldap_start_tls_s to %s failed with err %s!\n", options.urildap[i], ldap_err2string(res)); ldap_con = NULL; return res; } - } else { - ldap_con = ldap_init(options.hostname[i], options.port[i]); - res = ldap_simple_bind_s(ldap_con, options.binddn, options.password); - if (res == LDAP_SUCCESS) { - if (options.verbose&1 && strlen(options.urildap[i]) > 0) { - printf("Connected to %s as \"%s\"\n", options.urildap[i], options.binddn); - } else if (options.verbose&1) { - printf("Connected to %s:%d as \"%s\"\n", options.hostname[i], options.port[i], options.binddn); + + // Yes, you really do use ldap_sasl_bind_s() when doing a simple + // bind. This is apparently the "new" way, if not entirely obvious + if (strlen(options.binddn)) { + if (strlen(options.password)) { + creds->bv_len = strlen(options.password); + creds->bv_val = options.password; + } + // FIXME: Allow *real* SASL binds + if ((res = ldap_sasl_bind_s(ldap_con, options.binddn, NULL, creds, NULL, NULL, &msgid)) != LDAP_SUCCESS) { + fprintf(stderr, "LDAP bind problem:\n\t%s\n", ldap_err2string(res)); + fprintf(stderr, "Attempting to continue with anonymous credentials."); + res = LDAP_SUCCESS; } - return res; } } } - ldap_con = NULL; return res; } +void hosts2uri(void) +{ + int i, t; + // Convert any old host:port sets into URIs. This allows us + // to use the more modern ldap_initialize() instead of the + // deprecated ldap_init() + for (i = 0; i 0) { + t = options.useduris++; + snprintf(options.urildap[t], + sizeof(options.urildap[t]), + "ldap://%s:%d", + options.hostname[i], + options.port[i] ? options.port[i] : LDAP_PORT); + } + } +} + int main(int argc, char** argv) { @@ -1045,6 +1071,19 @@ int main(int argc, char** argv) main_argv = argv; parse_options(); + if (!options.output) { + fprintf(stderr, "[!!]\tMust select an output type (\"db\" or \"data\")\n"); + print_usage(); + exit(1); + } + + if (!strlen(options.searchbase)) { + fprintf(stderr, "[!!]\tMust provide the base DN for the search.\n"); + print_usage(); + exit(1); + } + + /* Initialization complete. If we're in daemon mode, fork and continue */ if (options.is_daemon) { if (options.is_daemon==1 && fork()) { @@ -1060,6 +1099,9 @@ int main(int argc, char** argv) set_datadir(); for (;;) { int ldaperr = -1; + + hosts2uri(); + res = do_connect(); if (res != LDAP_SUCCESS || ldap_con == NULL) { fprintf(stderr, "Warning - Problem while connecting to LDAP server:\n\t%s\n", ldap_err2string(res));