Wednesday, August 15, 2012

LDAP C Client Authentication Example (with OpenLDAP)

LDAP C Client Authentication Example (with OpenLDAP):
I have the goal of authenticate MySQL users with an LDAP server, currently, employees of my company are authenticated in several services (ftp, ssh, svn) through my LDAP server, except MySQL. (As you can imagine, I need to add manually every user in MySQL, a very tedious task).
In this post I only leave the example with LDAP authentication.
Installing necessary packages
yum groupinstall 'Development Tools'
yum install openldap-devel


Source ldapClient.c
#include <stdio.h>
#include <ldap.h>
/* LDAP Server settings */
#define LDAP_SERVER "ldap://nafiux.com:389"
int
main( int argc, char **argv )
{
LDAP        *ld;
int        rc;
char        bind_dn[100];

/* Get username and password */
if( argc != 3 )
{
perror( "invalid args, required: username password" );
return( 1 );
}
sprintf( bind_dn, "cn=%s,ou=People,dc=nafiux,dc=com", argv[1] );
printf( "Connecting as %s...\n", bind_dn );

/* Open LDAP Connection */
if( ldap_initialize( &ld, LDAP_SERVER ) )
{
perror( "ldap_initialize" );
return( 1 );
}

/* User authentication (bind) */
rc = ldap_simple_bind_s( ld, bind_dn, argv[2] );
if( rc != LDAP_SUCCESS )
{
fprintf(stderr, "ldap_simple_bind_s: %s\n", ldap_err2string(rc) );
return( 1 );
}
printf( "Successful authentication\n" );
ldap_unbind( ld );
return( 0 );
}

Compile and build
gcc ldapClient.c -o ldapClient -lldap

Run


PlanetMySQL Voting:
Vote UP /
Vote DOWN

DIGITAL JUICE

No comments:

Post a Comment

Thank's!