Active Directory/Connect: Difference between revisions

Added FreeBASIC
(Added FreeBASIC)
 
(7 intermediate revisions by 2 users not shown)
Line 1:
[[Category:Active Directory]]
{{task|Programming environment operations}}
 
 
The task is to establish a connection to an Active Directory or Lightweight Directory Access Protocol server.
Line 6 ⟶ 8:
{{works with|AutoHotkey_L}}
{{trans|VBScript}}
<langsyntaxhighlight AutoHotkeylang="autohotkey">objConn := CreateObject("ADODB.Connection")
objCmd := CreateObject("ADODB.Command")
objConn.Provider := "ADsDSOObject"
objConn.Open()</langsyntaxhighlight>
 
=={{header|AutoIt}}==
{{works with|AutoIt}}
<langsyntaxhighlight AutoItlang="autoit"> #include <AD.au3>
_AD_Open()</langsyntaxhighlight>
 
=={{header|C}}==
With OpenLDAP:
<langsyntaxhighlight Clang="c">#include <ldap.h>
...
char *name, *password;
Line 25 ⟶ 27:
ldap_simple_bind_s(ld, name, password);
... after done with it...
ldap_unbind(ld);</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">
// Requires adding a reference to System.DirectoryServices
var objDE = new System.DirectoryServices.DirectoryEntry("LDAP://DC=onecity,DC=corp,DC=fabrikam,DC=com");
</syntaxhighlight>
</lang>
 
=={{header|ColdFusion}}==
<langsyntaxhighlight lang="cfm">
<cfldap
server = "#someip#"
Line 45 ⟶ 47:
attributes = "#attributeslist#"
>
</syntaxhighlight>
</lang>
 
=={{header|D}}==
Based on dopenldap.
<syntaxhighlight lang="d">
<lang d>
import openldap;
import std.stdio;
Line 68 ⟶ 70:
}
</syntaxhighlight>
</lang>
 
=={{header|Erlang}}==
This needs a test case. Is there a LDAP server available?
<syntaxhighlight lang="erlang">
<lang Erlang>
-module(ldap_example).
-export( [main/1] ).
Line 80 ⟶ 82:
ok = eldap:simple_bind( Handle, DN, Password ),
eldap:close( Handle ).
</syntaxhighlight>
</lang>
 
=={{header|F_Sharp|F#}}==
{{trans|C_sharp}}
<p>For Active Directory we use the library System.DirectoryServices</p>
<langsyntaxhighlight lang="fsharp">let adObject = new System.DirectoryServices.DirectoryEntry("LDAP://DC=onecity,DC=corp,DC=fabrikam,DC=com")</langsyntaxhighlight>
<p>For your average LDAP server we use System.DirectoryServices.Protocol</p>
<p>For a minimal example we make an anonymous connect to the local machine on the well-known LDAP port 389
<langsyntaxhighlight lang="fsharp">let ldapServer = new System.DirectoryServices.Protocols.LdapDirectoryIdentifier("127.0.0.1")
let connect = new System.DirectoryServices.Protocols.LdapConnection(ldapServer)
connect.Bind()</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
{{libheader|winldap}}
<syntaxhighlight lang="vbnet">#Include "win\winldap.bi"
 
Dim ldap As LDAP Ptr
Dim hostname As String
Dim port As Integer
Dim username As String
Dim password As String
Dim result As Integer
 
hostname = "ldap.example.com"
port = 389 ' Standard port for LDAP. Use 636 for LDAPS.
username = "cn=username,dc=example,dc=com"
password = "password"
 
' Initialize the LDAP connection
ldap = ldap_init(hostname, port)
If ldap = NULL Then
Print "Error initializing LDAP connection"
Sleep
End 1
End If
 
' Authenticate with the LDAP server
result = ldap_simple_bind_s(ldap, username, password)
If result <> LDAP_SUCCESS Then
Print "Error authenticating with LDAP server: "; ldap_err2string(result)
Sleep
End 1
End If
 
' Here you can perform LDAP operations
'...
 
' We close the connection when finished
ldap_unbind(ldap)</syntaxhighlight>
 
=={{header|Go}}==
Line 96 ⟶ 136:
<br>
There are a large number of third-party LDAP libraries for Go. This uses one of the simpler ones and the code below is largely taken from the example on its main page.
<langsyntaxhighlight lang="go">package main
 
import (
Line 121 ⟶ 161:
}
// Do something
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
Line 127 ⟶ 167:
Example uses the [https://hackage.haskell.org/package/ldap-client <tt>ldap-client</tt>] package:
 
<langsyntaxhighlight lang="haskell">{-# LANGUAGE OverloadedStrings #-}
 
module Main (main) where
Line 141 ⟶ 181:
Ldap.search ldap (Ldap.Dn "o=example.com") (Ldap.typesOnly True) (Attr "uid" := Text.encodeUtf8 "user") []
for_ entries $ \entry ->
print entry</langsyntaxhighlight>
 
=={{header|Java}}==
Line 147 ⟶ 187:
This code uses the Apache Directory third-party library.
 
<langsyntaxhighlight lang="java">import java.io.IOException;
import org.apache.directory.api.ldap.model.exception.LdapException;
import org.apache.directory.ldap.client.api.LdapConnection;
Line 160 ⟶ 200:
}
}
}</langsyntaxhighlight>
 
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using LDAPClient
 
conn = LDAPClient.LDAPConnection("ldap://localhost:10389")
LDAPClient.simple_bind(conn, "user", "password")
LDAPClient.unbind(conn)
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">
import org.apache.directory.api.ldap.model.exception.LdapException
import org.apache.directory.ldap.client.api.LdapNetworkConnection
Line 216 ⟶ 256:
 
fun main(args: Array<String>) = LDAP(mapOf("hostname" to "localhost", "port" to "10389")).run()
</syntaxhighlight>
</lang>
 
=={{header|NetRexx}}==
Uses the [http://directory.apache.org/api/ Apache LDAP API], connecting to a local [http://directory.apache.org/apacheds/1.5/ ApacheDS] LDAP directory server.
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref symbols binary
 
Line 263 ⟶ 303:
 
return
</syntaxhighlight>
</lang>
 
'''Sample <tt>log4j.xml</tt> configuration file:'''
<langsyntaxhighlight lang="xml"><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
Line 292 ⟶ 332:
</root>
</log4j:configuration>
</syntaxhighlight>
</lang>
 
'''Output:'''
Line 301 ⟶ 341:
=={{header|Perl}}==
[http://search.cpan.org/dist/perl-ldap/|Perl LDAP Modules]
<langsyntaxhighlight 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 );
</syntaxhighlight>
</lang>
 
=={{header|Phix}}==
Line 313 ⟶ 353:
This has been tested against a random 7-year-old list of public ldap servers, getting mixed errors of
LDAP_SERVER_DOWN/LDAP_INVALID_CREDENTIALS/LDAP_INVALID_DN_SYNTAX/LDAP_CONFIDENTIALITY_REQUIRED.
<!--<langsyntaxhighlight Phixlang="phix">-->
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">/</span><span style="color: #000000;">ldap</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 329 ⟶ 369:
<span style="color: #000000;">ldap_unbind</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ld</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 337 ⟶ 377:
=={{header|PHP}}==
[http://php.net/ldap PHP LDAP Reference]
<langsyntaxhighlight lang="php"><?php
$ldap = ldap_connect($hostname, $port);
$success = ldap_bind($ldap, $username, $password);</langsyntaxhighlight>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="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")</langsyntaxhighlight>
 
=={{header|Python}}==
Line 353 ⟶ 393:
[http://www.python-ldap.org/doc/html/index.html python-ldap Documentation]
 
<langsyntaxhighlight lang="python">import ldap
 
l = ldap.initialize("ldap://ldap.example.com")
Line 363 ⟶ 403:
finally:
l.unbind()
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
This version uses the ldap package, and was tested against OpenLDAP (with real values):
<langsyntaxhighlight lang="racket">#lang racket
(require net/ldap)
(ldap-authenticate "ldap.somewhere.com" 389 "uid=username,ou=people,dc=somewhere,dc=com" password)</langsyntaxhighlight>
 
{{trans|C}}
Line 375 ⟶ 415:
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.)
 
<langsyntaxhighlight lang="racket">#lang racket
 
(require ffi/unsafe ffi/unsafe/define)
Line 390 ⟶ 430:
(ldap_simple_bind_s ld name password)
 
(ldap_unbind ld)</langsyntaxhighlight>
 
=={{header|Raku}}==
Line 397 ⟶ 437:
Using module LMDB - bindings to the openLDAP library. Requires an LDAP instance.
 
<syntaxhighlight lang="raku" perl6line>use LMDB;
 
my %DB := LMDB::DB.open(:path<some-dir>, %connection-parameters);
</syntaxhighlight>
</lang>
 
%DB may be accessed, read from and written to like a native hash.
Line 410 ⟶ 450:
 
{{libheader|RubyGems}}
<langsyntaxhighlight lang="ruby">require 'rubygems'
require 'net/ldap'
ldap = Net::LDAP.new(:host => 'ldap.example.com', :base => 'o=companyname')
ldap.authenticate('bind_dn', 'bind_pass')</langsyntaxhighlight>
 
=={{header|Run BASIC}}==
{{incorrect|Run BASIC|Active Directory has nothing to do with the local file system}}
<langsyntaxhighlight lang="runbasic">print shell$("dir") ' shell out to the os and print it</langsyntaxhighlight>
 
=={{header|Rust}}==
This solution uses the popular [https://crates.io/crates/ldap3 ldap3] crate.
<langsyntaxhighlight lang="rust">
let conn = ldap3::LdapConn::new("ldap://ldap.example.com")?;
conn.simple_bind("bind_dn", "bind_pass")?.success()?;
</syntaxhighlight>
</lang>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">import java.io.IOException
 
import org.apache.directory.api.ldap.model.exception.LdapException
Line 444 ⟶ 484:
}
}
}</langsyntaxhighlight>
 
=={{header|smart BASIC}}==
Line 450 ⟶ 490:
 
smart BASIC uses three separate commands to list the current directory, folder and files respectively.
<langsyntaxhighlight lang="qbasic">PRINT "Current directory: ";CURRENT_DIR$()
PRINT
PRINT "Folders:"
Line 464 ⟶ 504:
FOR n = 0 TO c-1
PRINT ,a$(n)
NEXT n</langsyntaxhighlight>
 
=={{header|Tcl}}==
This does not use SSPI/Kerberos yet, so your AD would need to allow simple ldap access.
<langsyntaxhighlight lang="tcl">package require ldap
set conn [ldap::connect $host $port]
ldap::bind $conn $user $password</langsyntaxhighlight>
 
=={{header|VBScript}}==
Creating the normal connection to AD
<langsyntaxhighlight lang="vbscript">Set objConn = CreateObject("ADODB.Connection")
Set objCmd = CreateObject("ADODB.Command")
objConn.Provider = "ADsDSOObject"
objConn.Open</langsyntaxhighlight>
 
=={{header|Wren}}==
Line 483 ⟶ 523:
{{libheader|OpenLDAP}}
As it's not currently possible for Wren-cli to access OpenLDAP directly, we embed a Wren script in a C application to complete this task.
<langsyntaxhighlight ecmascriptlang="wren">/* active_directory_connectActive_Directory_Connect.wren */
 
foreign class LDAP {
Line 509 ⟶ 549:
}
 
var ld = LDAP.newinit("ldap.somewhere.com", 389)
ld.simpleBindS(ld, name, password)
 
// do something here
 
ld.unbind()</langsyntaxhighlight>
<br>
We now embed this in the following C program, compile and run it.
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdio_ext.h>
#include <stdlib.h>
Line 571 ⟶ 611:
if (strcmp(module, "main") == 0) {
if (strcmp(className, "LDAP") == 0) {
if (!isStatic && strcmp(signature, "simpleBindS(_,_)") == 0) return C_simpleBindS;
if (!isStatic && strcmp(signature, "unbind()") == 0) return C_unbind;
} else if (strcmp(className, "C") == 0) {
Line 619 ⟶ 659:
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "active_directory_connectActive_Directory_Connect.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
Line 635 ⟶ 675:
free(script);
return 0;
}</langsyntaxhighlight>
 
{{omit from|Active Directory}}
Line 644 ⟶ 684:
{{omit from|Lilypond}}
{{omit from|Lingo}}
{{omit from|TI-83 BASIC}}
{{omit from|TI-89 BASIC}} <!-- Does not have network access. -->
{{omit from|Mathematica}}
{{omit from|Maxima}}
{{omit from|MIPS Assembly|None of the commonly used implementations can access AD functions}}
{{omit from|ML/I}}
Line 653 ⟶ 692:
{{omit from|Retro}}
{{omit from|SNOBOL4|Does not have network access.}}
{{omit from|TI-83 BASIC}}
{{omit from|TI-89 BASIC}} <!-- Does not have network access. -->
{{omit from|Yorick|Does not have network access.}}
{{omit from|ZX Spectrum Basic|Does not have network access.}}
{{omit from|Maxima}}
 
[[Category:Active Directory]]
2,130

edits