Active Directory/Connect

From Rosetta Code
(Redirected from Connect to Active Directory)
Jump to: navigation, search
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.

Contents

[edit] AutoIt

Works with: AutoIt
 #include <AD.au3>
_AD_Open()

[edit] AutoHotkey

Works with: AutoHotkey_L
Translation of: VBScript
objConn := CreateObject("ADODB.Connection")
objCmd := CreateObject("ADODB.Command")
objConn.Provider := "ADsDSOObject"
objConn.Open()

[edit] C

With OpenLDAP:

#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);

[edit] Erlang

This needs a test case. Is there a LDAP server available?

 
-module(ldap_example).
-export( [main/1] ).
 
main( [Host, DN, Password] ) ->
{ok, Handle} = eldap:open( [Host] ),
ok = eldap:simple_bind( Handle, DN, Password ),
eldap:close( Handle ).
 

[edit] Java

Translation of: NetRexx
 
import java.io.IOException;
 
import org.apache.directory.ldap.client.api.LdapConnection;
import org.apache.directory.ldap.client.api.LdapNetworkConnection;
import org.apache.directory.shared.ldap.model.cursor.EntryCursor;
import org.apache.directory.shared.ldap.model.entry.Entry;
import org.apache.directory.shared.ldap.model.exception.LdapException;
import org.apache.directory.shared.ldap.model.message.SearchScope;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
public class RDirectoryLDAP {
 
static final Logger log_;
private static final String ldapHostName;
private static final int ldapPort;
private static LdapConnection connection;
 
static {
log_ = LoggerFactory.getLogger(RDirectoryLDAP.class);
connection = null;
ldapHostName = "localhost";
ldapPort = 10389;
}
 
public static void main(String[] args) {
try {
if (log_.isInfoEnabled()) { log_.info("LDAP Connection to " + ldapHostName + " on port " + ldapPort); }
connection = new LdapNetworkConnection(ldapHostName, ldapPort);
 
try {
if (log_.isTraceEnabled()) { log_.trace("LDAP bind"); }
connection.bind();
 
if (log_.isTraceEnabled()) { log_.trace("LDAP unbind"); }
connection.unBind();
}
catch (LdapException lex) {
log_.error("LDAP Error", lex);
}
catch (IOException ex) {
log_.error("I/O Error", ex);
}
}
finally {
if (log_.isTraceEnabled()) { log_.trace("LDAP close connection"); }
try {
if (connection != null) {
connection.close();
}
}
catch (IOException ex) {
log_.error("I/O Error on connection.close()", ex);
}
}
 
return;
}
}
 

[edit] NetRexx

Uses the Apache LDAP API, connecting to a local ApacheDS LDAP directory server.

/* 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
 

Sample log4j.xml configuration file:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{HH:mm:ss}] %-5p [%c] - %m%n" />
</layout>
</appender>
 
<!-- with these we'll not get innundated when switching to DEBUG -->
<logger name="org.apache.directory.shared.ldap.name">
<level value="warn" />
</logger>
<logger name="org.apache.directory.shared.codec">
<level value="warn" />
</logger>
<logger name="org.apache.directory.shared.asn1">
<level value="warn" />
</logger>
 
<root>
<level value="info" />
<appender-ref ref="stdout" />
</root>
</log4j:configuration>
 

Output:

[08:40:05] INFO  [RDirectoryLDAP] - LDAP Connection to localhost on port 10389

[edit] Perl

LDAP Modules

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

[edit] PHP

PHP LDAP Reference

<?php
$ldap = ldap_connect($hostname, $port);
$success = ldap_bind($ldap, $username, $password);

[edit] 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")

[edit] Python

Works with: Python version 2.6
Library: python-ldap

python-ldap Documentation

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("[email protected]", "password")
finally:
l.unbind()
 

[edit] Racket

Translation of: C

This is a direct translation of the C code -- I have no idea how to try it out since I don't have a working ldap server... So take it as a stub that waits for someone who can try it to do so. (And it's a low level thing anyway, there's an ldap package for Racket which I can't try for a similar reason.)

 
#lang racket
 
(require ffi/unsafe ffi/unsafe/define)
 
(define-ffi-definer defldap (ffi-lib "libldap"))
(defldap ldap_init (_fun _string _int -> _pointer))
(defldap ldap_unbind (_fun _pointer -> _void))
(defldap ldap_simple_bind_s (_fun _pointer _string _string -> _int))
(defldap ldap_err2string (_fun _int -> _string))
 
(define name ...)
(define password ...)
(define ld (ldap_init "ldap.somewhere.com" 389))
(ldap_simple_bind_s ld name password)
 
(ldap_unbind ld)
 

[edit] 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
require 'rubygems'
require 'net/ldap'
ldap = Net::LDAP.new(:host => 'ldap.example.com', :base => 'o=companyname')
ldap.authenticate('bind_dn', 'bind_pass')

[edit] Run BASIC

print shell$("dir") ' shell out to the os and print it

[edit] Tcl

This does not use SSPI/Kerberos yet, so your AD would need to allow simple ldap access.

package require ldap
set conn [ldap::connect $host $port]
ldap::bind $conn $user $password

[edit] VBScript

Creating the normal connection to AD

Set objConn = CreateObject("ADODB.Connection")
Set objCmd = CreateObject("ADODB.Command")
objConn.Provider = "ADsDSOObject"
objConn.Open
Personal tools
Namespaces

Variants
Actions
Community
Explore
Misc
Toolbox