Solved

LDAP in C++

Posted on 2006-06-30
2
2,717 Views
Last Modified: 2008-02-01
Hi,
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:
#!usr/local/bin/perl      
use  Net::LDAP;
use Term::ReadKey;

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



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

print " Enter Password: ";
ReadMode('noecho');
$password = <STDIN>;
chop($password);
ReadMode('normal');


$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
if(length($password)==0)
      {
      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(!$ldap)
      {
      # 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;
if(!$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";
      }
else
      {
      print "\nInvalid PASSWORD.\n";
      }
$ldap->unbind();
0
Comment
Question by:niravdesai23
2 Comments
 

Author Comment

by:niravdesai23
Comment Utility
I increased the points....anybody with any clue on this...
0
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
Comment Utility
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;
                        break;
                  }

                  (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 ) {
                  break;
            }
      }

      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;
}
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now