Active Directory/Connect: Difference between revisions

Added FreeBASIC
m (→‎{{header|Run BASIC}}: mark incorrect)
(Added FreeBASIC)
 
(17 intermediate revisions by 6 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.
 
=={{header|AutoIt}}==
{{works with|AutoIt}}
<lang AutoIt> #include <AD.au3>
_AD_Open()</lang>
 
=={{header|AutoHotkey}}==
{{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}}
<syntaxhighlight lang="autoit"> #include <AD.au3>
_AD_Open()</syntaxhighlight>
 
=={{header|C}}==
With OpenLDAP:
<langsyntaxhighlight Clang="c">#include <ldap.h>
...
char *name, *password;
Line 24 ⟶ 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 69 ⟶ 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 81 ⟶ 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 97 ⟶ 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 122 ⟶ 161:
}
// Do something
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
Line 128 ⟶ 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 142 ⟶ 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 148 ⟶ 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 161 ⟶ 200:
}
}
}</langsyntaxhighlight>
 
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">using LDAPClient
 
conn = LDAPClient.LDAPConnection("ldap://localhost:10389")
LDAPClient.simple_bind(conn, "user", "password")
LDAPClient.unbind(conn)
</syntaxhighlight>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">
import org.apache.directory.api.ldap.model.exception.LdapException
import org.apache.directory.ldap.client.api.LdapNetworkConnection
Line 208 ⟶ 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 255 ⟶ 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 284 ⟶ 332:
</root>
</log4j:configuration>
</syntaxhighlight>
</lang>
 
'''Output:'''
Line 293 ⟶ 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|Perl 6Phix}}==
{{trans|C}}
Using module LMDB - bindings to the openLDAP library. Requires an LDAP instance.
Note that builtins\ldap.e is incomplete, windows-only (depends on wldap32.dll), and largely untested.
 
This has been tested against a random 7-year-old list of public ldap servers, getting mixed errors of
<lang perl6>use LMDB;
LDAP_SERVER_DOWN/LDAP_INVALID_CREDENTIALS/LDAP_INVALID_DN_SYNTAX/LDAP_CONFIDENTIALITY_REQUIRED.
 
<!--<syntaxhighlight lang="phix">-->
my %DB := LMDB::DB.open(:path<some-dir>, %connection-parameters);
<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>
</lang>
 
<span style="color: #008080;">constant</span> <span style="color: #000000;">servers</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span>
%DB may be accessed, read from and written to like a native hash.
<span style="color: #008000;">"ldap.somewhere.com"</span><span style="color: #0000FF;">,</span>
<span style="color: #0000FF;">}</span>
<span style="color: #000080;font-style:italic;">--...</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">name</span><span style="color: #0000FF;">=</span><span style="color: #008000;">"name"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">password</span><span style="color: #0000FF;">=</span><span style="color: #008000;">"passwd"</span>
<span style="color: #000080;font-style:italic;">--...</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">servers</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">ld</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ldap_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">servers</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ldap_simple_bind_s</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ld</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">name</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">password</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s: %d [%s]\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">servers</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ldap_err_desc</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)})</span>
<span style="color: #000080;font-style:italic;">--... after done with it...</span>
<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>
<!--</syntaxhighlight>-->
{{out}}
<pre>
ldap.somewhere.com: 81 [LDAP_SERVER_DOWN]
</pre>
 
=={{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 328 ⟶ 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 338 ⟶ 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 350 ⟶ 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 365 ⟶ 430:
(ldap_simple_bind_s ld name password)
 
(ldap_unbind ld)</langsyntaxhighlight>
 
=={{header|RingRaku}}==
(formerly Perl 6)
{{incorrect|Ring|Active Directory has nothing to do with the local file system}}
 
<lang ring>
Using module LMDB - bindings to the openLDAP library. Requires an LDAP instance.
see system("dir") + nl
 
</lang>
<syntaxhighlight lang="raku" line>use LMDB;
 
my %DB := LMDB::DB.open(:path<some-dir>, %connection-parameters);
</syntaxhighlight>
 
%DB may be accessed, read from and written to like a native hash.
 
=={{header|Ruby}}==
Line 379 ⟶ 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.
<syntaxhighlight lang="rust">
let conn = ldap3::LdapConn::new("ldap://ldap.example.com")?;
conn.simple_bind("bind_dn", "bind_pass")?.success()?;
</syntaxhighlight>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">import java.io.IOException
 
import org.apache.directory.api.ldap.model.exception.LdapException
Line 406 ⟶ 484:
}
}
}</langsyntaxhighlight>
 
=={{header|smart BASIC}}==
{{incorrect|smart BASIC|Active Directory has nothing to do with the local file system}}
 
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 424 ⟶ 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}}==
{{trans|C}}
{{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.
<syntaxhighlight lang="wren">/* Active_Directory_Connect.wren */
 
foreign class LDAP {
construct init(host, port) {}
 
foreign simpleBindS(name, password)
 
foreign unbind()
}
 
class C {
foreign static getInput(maxSize)
}
 
var name = ""
while (name == "") {
System.write("Enter name : ")
name = C.getInput(40)
}
 
var password = ""
while (password == "") {
System.write("Enter password : ")
password = C.getInput(40)
}
 
var ld = LDAP.init("ldap.somewhere.com", 389)
ld.simpleBindS(name, password)
 
// do something here
 
ld.unbind()</syntaxhighlight>
<br>
We now embed this in the following C program, compile and run it.
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <string.h>
#include <ldap.h>
#include "wren.h"
 
/* C <=> Wren interface functions */
 
void C_ldapAllocate(WrenVM* vm) {
LDAP** pldap = (LDAP**)wrenSetSlotNewForeign(vm, 0, 0, sizeof(LDAP*));
char *host = (char *)wrenGetSlotString(vm, 1);
int port = (int)wrenGetSlotDouble(vm, 2);
*pldap = ldap_init(host, port);
}
 
void C_simpleBindS(WrenVM* vm) {
LDAP* ldap = *(LDAP**)wrenGetSlotForeign(vm, 0);
const char *name = wrenGetSlotString(vm, 1);
const char *password = wrenGetSlotString(vm, 2);
ldap_simple_bind_s(ldap, name, password);
}
 
void C_unbind(WrenVM* vm) {
LDAP* ldap = *(LDAP**)wrenGetSlotForeign(vm, 0);
ldap_unbind(ldap);
}
 
void C_getInput(WrenVM* vm) {
int maxSize = (int)wrenGetSlotDouble(vm, 1) + 2;
char input[maxSize];
fgets(input, maxSize, stdin);
__fpurge(stdin);
input[strcspn(input, "\n")] = 0;
wrenSetSlotString(vm, 0, (const char*)input);
}
 
WrenForeignClassMethods bindForeignClass(WrenVM* vm, const char* module, const char* className) {
WrenForeignClassMethods methods;
methods.finalize = NULL;
if (strcmp(className, "LDAP") == 0) {
methods.allocate = C_ldapAllocate;
}
return methods;
}
 
WrenForeignMethodFn bindForeignMethod(
WrenVM* vm,
const char* module,
const char* className,
bool isStatic,
const char* signature) {
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) {
if (isStatic && strcmp(signature, "getInput(_)") == 0) return C_getInput;
}
}
return NULL;
}
 
static void writeFn(WrenVM* vm, const char* text) {
printf("%s", text);
}
 
void errorFn(WrenVM* vm, WrenErrorType errorType, const char* module, const int line, const char* msg) {
switch (errorType) {
case WREN_ERROR_COMPILE:
printf("[%s line %d] [Error] %s\n", module, line, msg);
break;
case WREN_ERROR_STACK_TRACE:
printf("[%s line %d] in %s\n", module, line, msg);
break;
case WREN_ERROR_RUNTIME:
printf("[Runtime Error] %s\n", msg);
break;
}
}
 
char *readFile(const char *fileName) {
FILE *f = fopen(fileName, "r");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
rewind(f);
char *script = malloc(fsize + 1);
fread(script, 1, fsize, f);
fclose(f);
script[fsize] = 0;
return script;
}
 
int main(int argc, char **argv) {
WrenConfiguration config;
wrenInitConfiguration(&config);
config.writeFn = &writeFn;
config.errorFn = &errorFn;
config.bindForeignClassFn = &bindForeignClass;
config.bindForeignMethodFn = &bindForeignMethod;
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "Active_Directory_Connect.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
switch (result) {
case WREN_RESULT_COMPILE_ERROR:
printf("Compile Error!\n");
break;
case WREN_RESULT_RUNTIME_ERROR:
printf("Runtime Error!\n");
break;
case WREN_RESULT_SUCCESS:
break;
}
wrenFreeVM(vm);
free(script);
return 0;
}</syntaxhighlight>
 
{{omit from|Active Directory}}
Line 446 ⟶ 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 455 ⟶ 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,122

edits