Active Directory/Connect

From Rosetta Code
Revision as of 01:25, 30 September 2012 by rosettacode>Alansam (Add NetRexx implementation)
Task
Active Directory/Connect
You are encouraged to solve this task according to the task description, using any language you may know.

The task is to establish a connection to an Active Directory or Lightweight Directory Access Protocol server.

AutoHotkey

Works with: AutoHotkey_L
Translation of: VBScript

<lang AutoHotkey>objConn := CreateObject("ADODB.Connection") objCmd := CreateObject("ADODB.Command") objConn.Provider := "ADsDSOObject" objConn.Open()</lang>

C

With OpenLDAP: <lang C>#include <ldap.h> ... char *name, *password; ... LDAP *ld = ldap_init("ldap.somewhere.com", 389); ldap_simple_bind_s(ld, name, password); ... after done with it... ldap_unbind(ld);</lang>

NetRexx

Uses the Apache LDAP API. <lang NetRexx>/* NetRexx */ options replace format comments java crossref symbols binary

import org.apache.directory.ldap.client.api.LdapConnection import org.apache.directory.ldap.client.api.LdapNetworkConnection import org.apache.directory.shared.ldap.model.exception.LdapException import org.slf4j.Logger import org.slf4j.LoggerFactory

class RDirectoryLDAP public

 properties constant
   log_ = LoggerFactory.getLogger(RDirectoryLDAP.class)
 properties private static
   connection = LdapConnection null
 method main(args = String[]) public static
   ldapHostName = String "localhost"
   ldapPort = int 10389
   if log_.isInfoEnabled() then log_.info("LDAP Connection to" ldapHostName "on port" ldapPort)
   connection = LdapNetworkConnection(ldapHostName, ldapPort)
   do
     if log_.isTraceEnabled() then log_.trace("LDAP bind")
     connection.bind()
     if log_.isTraceEnabled() then log_.trace("LDAP unbind")
     connection.unBind()
   catch lex = LdapException
     log_.error("LDAP Error", Throwable lex)
   catch iox = IOException
     log_.error("I/O Error", Throwable iox)
   finally
     do
     if connection \= null then connection.close()
     catch iox = IOException
       log_.error("I/O Error on connection.close()", Throwable iox)
     end
   end
   return

</lang>

Perl

LDAP Modules <lang perl> use Net::LDAP;

my $ldap = Net::LDAP->new('ldap://ldap.example.com') or die $@; my $mesg = $ldap->bind( $bind_dn, password => $bind_pass ); </lang>

PHP

PHP LDAP Reference <lang php><?php $ldap = ldap_connect($hostname, $port); $success = ldap_bind($ldap, $username, $password);</lang>

PicoLisp

<lang PicoLisp>(unless (=0 (setq Ldap (native "libldap.so" "ldap_open" 'N "example.com" 389)))

  (quit "Can't open LDAP") )

(native "libldap.so" "ldap_simple_bind_s" 'I Ldap "user" "password")</lang>

Python

Works with: Python version 2.6
Library: python-ldap

python-ldap Documentation

<lang python>import ldap

l = ldap.initialize("ldap://ldap.example.com") try:

   l.protocol_version = ldap.VERSION3
   l.set_option(ldap.OPT_REFERRALS, 0)
   bind = l.simple_bind_s("me@example.com", "password")

finally:

   l.unbind()

</lang>

Ruby

Similar to Tcl, assume the AD server talks LDAP.

There are many Ruby LDAP packages ([1]) -- this solution uses Net::LDAP ("Pure Ruby LDAP Tools" on RubyForge, gem name "ruby-net-ldap")

Library: RubyGems

<lang ruby>require 'rubygems' require 'net/ldap' ldap = Net::LDAP.new(:host => 'ldap.example.com', :base => 'o=companyname') ldap.authenticate('bind_dn', 'bind_pass')</lang>

Run BASIC

<lang runbasic>print shell$("dir") ' shell out to the os and print it</lang>

Tcl

This does not use SSPI/Kerberos yet, so your AD would need to allow simple ldap access. <lang tcl>package require ldap set conn [ldap::connect $host $port] ldap::bind $conn $user $password</lang>

VBScript

Creating the normal connection to AD <lang vbscript>Set objConn = CreateObject("ADODB.Connection") Set objCmd = CreateObject("ADODB.Command") objConn.Provider = "ADsDSOObject" objConn.Open</lang>