[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


LDAP in C++

Posted on 2006-06-30
Medium Priority
Last Modified: 2008-02-01
I have PERL code for LDAP that take userid and password...and authenticate the user. Now I want to make the C++ code for the same. How can I make that?
Perl code:
use  Net::LDAP;
use Term::ReadKey;

$user= "";
$password= "";

print " Enter Username: ";
$user =<STDIN>;

print " Enter Password: ";
$password = <STDIN>;

$LDAP_HOST            = 'ids.gm.com';
$LDAP_PORT            = 389;
$LDAP_BASE_DN             = 'ou=people,ou=intranet,dc=gm,dc=com';
$LDAP_USER_ATTRIBUTE      = 'gmguid';
$TRUE = 1;

# See if the Password is NULL
      print "\nPassword field is left EMPTY.\n";

#Establish a connection to the LDAP Server
my $ldap = new Net::LDAP($LDAP_HOST, port=>$LDAP_PORT, onerror=>'undef');
      # If the Connection is unsuccessful      
      print "\nConnection to LDAP Server is UNSUCCESFUL.\n";    

#Validate the User ID and Get its Distinguished Name
my $dn = $LDAP_USER_ATTRIBUTE . '=' . $user . ',' . $LDAP_BASE_DN;
      # No Entry exists that matches $USERDN      
      print "\nInvalid USERID.\n";    

#Authenticate the User       
if (defined($ldap->bind($dn, password=>$password)))
      print "\nUSER is AUTHINTICATED.\n";
      print "\nInvalid PASSWORD.\n";
Question by:niravdesai23
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Author Comment

ID: 17019327
I increased the points....anybody with any clue on this...
LVL 86

Accepted Solution

jkr earned 600 total points
ID: 17019402
OpenLDAP (http://www.openldap.org/) is a library that you'll need and find helpful for doing LDAP in C/C++. For the above, see http://www.openldap.org/devel/cvsweb.cgi/~checkout~/tests/progs/slapd-bind.c?rev=1.24&hideattic=1&sortbydate=0

The relevant part is

static int
do_bind( char *uri, char *dn, struct berval *pass, int maxloop,
      int force, int chaserefs, int noinit, LDAP **ldp )
      LDAP      *ld = ldp ? *ldp : NULL;
      int        i, rc = -1;
      pid_t      pid = getpid();

      if ( maxloop > 1 )
            fprintf( stderr, "PID=%ld - Bind(%d): dn=\"%s\".\n",
                   (long) pid, maxloop, dn );

      for ( i = 0; i < maxloop; i++ ) {
            if ( !noinit || ld == NULL ) {
                  int version = LDAP_VERSION3;
                  ldap_initialize( &ld, uri );
                  if ( ld == NULL ) {
                        tester_perror( "ldap_initialize", NULL );
                        rc = -1;

                  (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
                        &version );
                  (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
                        chaserefs ? LDAP_OPT_ON: LDAP_OPT_OFF );

            rc = ldap_sasl_bind_s( ld, dn, LDAP_SASL_SIMPLE, pass, NULL, NULL, NULL );
            if ( rc ) {
                  unsigned first = tester_ignore_err( rc );

                  /* if ignore.. */
                  if ( first ) {
                        /* only log if first occurrence */
                        if ( force < 2 || first == 1 ) {
                              tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
                        rc = LDAP_SUCCESS;

                  } else {
                        tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
            if ( !noinit ) {
                  ldap_unbind_ext( ld, NULL, NULL );
                  ld = NULL;

            if ( rc != LDAP_SUCCESS ) {

      if ( maxloop > 1 ) {
            fprintf( stderr, " PID=%ld - Bind done (%d).\n", (long) pid, rc );

      if ( ldp && noinit ) {
            *ldp = ld;

      } else if ( ld != NULL ) {
            ldap_unbind_ext( ld, NULL, NULL );

      return rc;

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question