Active Directory/Search for a user: Difference between revisions

Added FreeBASIC
(Add UNIX Shell implementation)
(Added FreeBASIC)
 
(33 intermediate revisions by 15 users not shown)
Line 1:
[[Category:Active Directory]]
{{task|Programming environment operations}}
 
Line 4 ⟶ 5:
 
=={{header|C}}==
<langsyntaxhighlight Clang="c">#include <ldap.h>
 
char *name, *password;
Line 24 ⟶ 25:
 
ldap_msgfree(*result); /* free messages */
ldap_unbind(ld); /* disconnect */</langsyntaxhighlight>
 
=={{header|JavaD}}==
Based on dopenldap.
{{Trans|NetRexx}}
<syntaxhighlight lang="d">
<lang java>
import java.io.IOExceptionopenldap;
import javastd.util.Arraysstdio;
 
void main() {
import org.apache.directory.ldap.client.api.LdapConnection;
// connect to server
import org.apache.directory.ldap.client.api.LdapNetworkConnection;
auto ldap = LDAP("ldap://localhost");
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;
 
// search for uid
auto r = ldap.search_s("dc=example,dc=com", LDAP_SCOPE_SUBTREE, "(uid=%s)".format("test"));
 
// show properties
public class RDirectorySearchLDAP {
writeln("Found dn: %s", r[0].dn);
foreach(k, v; r[0].entry)
writeln("%s = %s", k, v);
 
// bind on found entry
protected static final Logger log_;
int b = ldap.bind_s(r[0].dn, "password");
private static LdapConnection connection;
scope(exit) ldap.unbind;
private static final String ldapHostName;
if (b)
private static final int ldapPort;
{
private static final String ldapDnStr;
writeln("error on binding");
private static final String ldapCreds;
return;
}
 
// do something
static {
...
}
</syntaxhighlight>
 
=={{header|Eiffel}}==
log_ = LoggerFactory.getLogger(RDirectorySearchLDAP.class);
Eiffel does not have the notion of "return", but "Result". A consequence of this is that Eiffel routines are Single-entry-Single-exit, which means less bugs. In the example (below), the Result is of type BOOLEAN.
 
Moreover, strings in Eiffel are objects and cannot be directly passed to the Windows OS. As such, they need to undergo a format change through the facilities of a WEL_STRING, which makes the appropriate structure conversion.
ldapHostName = "localhost";
<syntaxhighlight lang="eiffel">
ldapPort = 11389;
feature -- Validation
ldapDnStr="uid=admin,ou=system";
ldapCreds="********";
}
 
is_user_credential_valid (a_domain, a_username, a_password: READABLE_STRING_GENERAL): BOOLEAN
public static void main(String[] args) {
-- Is the pair `a_username'/`a_password' a valid credential in `a_domain'?
local
l_domain, l_username, l_password: WEL_STRING
do
create l_domain.make (a_domain)
create l_username.make (a_username)
create l_password.make (a_password)
Result := cwel_is_credential_valid (l_domain.item, l_username.item, l_password.item)
end
</syntaxhighlight>
 
Because Active Directory is a Windows OS facility, in Eiffel we must use the WEL (Windows Eiffel Library) components. Thus, the code above is not cross-platform. Moreover, the call to `cwel_is_credential_valid' is shown below:
boolean connected = false;
 
<syntaxhighlight lang="eiffel">
try {
cwel_is_credential_valid (a_domain, a_username, a_password: POINTER): BOOLEAN
connected = setUp();
external
if (connected) {
"C inline use %"wel_user_validation.h%""
search("*mil*");
alias
}
"return cwel_is_credential_valid ((LPTSTR) $a_domain, (LPTSTR) $a_username, (LPTSTR) $a_password);"
}
end
finally {
</syntaxhighlight>
if (connected) {
tearDown();
}
}
 
=={{header|FreeBASIC}}==
return;
See [https://rosettacode.org/wiki/Active_Directory/Connect#FreeBASIC Active_Directory/Connect#FreeBASIC]
}
 
=={{header|Go}}==
private static boolean search(String uid) {
{{libheader|go-ldap-client}}
<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.
<syntaxhighlight lang="go">package main
 
import (
boolean state;
"log"
EntryCursor cursor;
"github.com/jtblin/go-ldap-client"
Entry ev;
)
String baseDn;
String filter;
SearchScope scope;
String attributes[];
int ksearch = 0;
 
func main() {
state = true;
client := &ldap.LDAPClient{
 
baseDn = Base: "oudc=usersexample,odc=mojocom";,
Host: "ldap.example.com",
filter = "(&(objectClass=person)(&(uid=" + uid + ")))";
Port: 389,
scope = SearchScope.SUBTREE;
GroupFilter: "(memberUid=%s)",
attributes = new java.lang.String[] { "dn", "cn", "sn", "uid" };
 
try {
if (log_.isTraceEnabled()) { log_.trace("LDAP search"); }
if (log_.isInfoEnabled()) {
log_.info("Begin search");
log_.info(" search base distinguished name: " + baseDn);
log_.info(" search filter: " + filter);
log_.info(" search attributes: " + (Arrays.asList(attributes).toString()));
}
cursor = connection.search(baseDn, filter, scope, attributes);
while (cursor.next()) {
ksearch++;
ev = cursor.get();
if (log_.isInfoEnabled()) { log_.info("Search cursor entry count: " + ksearch); }
if (log_.isInfoEnabled()) { log_.info(ev.toString()); }
}
}
defer client.Close()
catch (LdapException lex) {
err state := false;client.Connect()
if err != nil {
log_.error("LDAP Error in cursor loop: Iteration " + ksearch, lex);
log.Fatalf("Failed to connect : %+v", err)
}
groups, err := client.GetGroupsOfUser("username")
catch (Exception ex) {
if err state != false;nil {
log_ log.errorFatalf("I/O Error ingetting cursorgroups loop:for Iterationuser "%s: %+v", ksearch"username", exerr);
}
log.Printf("Groups: %+v", groups)
}</syntaxhighlight>
 
=={{header|Haskell}}==
return state;
}
 
Example uses the [https://hackage.haskell.org/package/ldap-client <tt>ldap-client</tt>] package:
private static boolean search() {
return search("*");
}
 
<syntaxhighlight lang="haskell">{-# LANGUAGE OverloadedStrings #-}
private static boolean setUp() {
 
module Main (main) where
boolean state = false;
 
import Data.Foldable (for_)
try {
import qualified Data.Text.Encoding as Text (encodeUtf8)
if (log_.isInfoEnabled()) { log_.info("LDAP Connection to " + ldapHostName + " on port " + ldapPort); }
import Ldap.Client (Attr(..), Filter(..))
connection = new LdapNetworkConnection(ldapHostName, ldapPort);
import qualified Ldap.Client as Ldap (Dn(..), Host(..), search, with, typesOnly)
 
main :: IO ()
if (log_.isTraceEnabled()) { log_.trace("LDAP bind"); }
main = do
connection.bind(ldapDnStr, ldapCreds);
entries <- Ldap.with (Ldap.Plain "localhost") 389 $ \ldap ->
Ldap.search ldap (Ldap.Dn "o=example.com") (Ldap.typesOnly True) (Attr "uid" := Text.encodeUtf8 "user") []
for_ entries $ \entry ->
print entry</syntaxhighlight>
 
=={{header|Java}}==
state = true;
}
catch (LdapException lex) {
state = false;
log_.error("LDAP Error", lex);
}
catch (IOException iox) {
state = false;
log_.error("I/O Error", iox);
}
 
The following code uses the Apache Directory project, version 1.0.0.
return state;
}
 
<syntaxhighlight lang="java">import java.io.IOException;
private static boolean tearDown() {
import org.apache.directory.api.ldap.model.cursor.CursorException;
import org.apache.directory.api.ldap.model.cursor.EntryCursor;
import org.apache.directory.api.ldap.model.entry.Entry;
import org.apache.directory.api.ldap.model.exception.LdapException;
import org.apache.directory.api.ldap.model.message.SearchScope;
import org.apache.directory.ldap.client.api.LdapConnection;
import org.apache.directory.ldap.client.api.LdapNetworkConnection;
 
public class LdapSearchDemo {
boolean state = false;
 
public static void main(String[] args) throws IOException, LdapException, CursorException {
try {
new LdapSearchDemo().demonstrateSearch();
if (log_.isTraceEnabled()) { log_.trace("LDAP unbind"); }
connection.unBind();
state = true;
}
 
catch (LdapException lex) {
private void demonstrateSearch() throws IOException, LdapException, CursorException {
state = false;
try (LdapConnection conn = new LdapNetworkConnection("localhost", 11389)) {
log_.error("LDAP Error", lex);
conn.bind("uid=admin,ou=system", "********");
search(conn, "*mil*");
conn.unBind();
}
}
 
finally {
private void search(LdapConnection connection, String uid) throws LdapException, CursorException {
try {
connection.close()String baseDn = "ou=users,o=mojo";
String filter = "(&(objectClass=person)(&(uid=" + uid + ")))";
}
SearchScope scope = SearchScope.SUBTREE;
catch (IOException iox) {
stateString[] attributes = false{"dn", "cn", "sn", "uid"};
int ksearch = 0;
log_.error("I/O Error on connection.close()", iox);
 
}
EntryCursor cursor = connection.search(baseDn, filter, scope, attributes);
while (cursor.next()) {
ksearch++;
Entry entry = cursor.get();
System.out.printf("Search entry %d = %s%n", ksearch, entry);
}
}
}</syntaxhighlight>
 
=={{header|Julia}}==
return state;
<syntaxhighlight lang="julia">using LDAPClient
}
 
}
function searchLDAPusers(searchstring, uname, pword, host=["example", "com"])
</lang>
conn = LDAPClient.LDAPConnection("ldap://ldap.server.net")
LDAPClient.simple_bind(conn, uname, pword)
 
search_string = "CN=Users,DC=$(host[1]),DC=$(host[2])"
=={{header|PicoLisp}}==
scope = LDAPClient.LDAP_SCOPE_ONELEVEL
<lang PicoLisp>(de ldapsearch (Sn)
chain = LDAPClient.search(conn, search_string, scope, filter="(&(objectClass=person)(&(uid=$(searchstring))))")
(in
 
(list "ldapsearch" "-xH" "ldap://db.debian.org"
for entry in LDAPClient.each_entry(chain)
"-b" "dc=debian,dc=org"
println("Search for $searchstring found user $(entry["name"]) with attributes:")
(pack "sn=" Sn) )
for attr in LDAPClient.each_attribute(entry)
(list
(cons 'cn (prog println(from "cn: ") (line T)) ", attr)
end
(cons 'uid (prog (from "uid: ") (line T))) ) ) )</lang>
end
Test:
 
<pre>: (ldapsearch "Fischer")
LDAPClient.unbind(conn)
-> ((cn . "Mika") (uid . "mf"))</pre>
end
 
searchLDAPusers("Mario", "my-username", "my-password")
</syntaxhighlight>
 
=={{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 318 ⟶ 331:
 
return state
</syntaxhighlight>
</lang>
'''Output:'''
<pre>
Line 332 ⟶ 345:
sn: Milton
cn: John Milton
</pre>
 
=={{header|ooRexx}}==
Using LDAP connecting to a local [http://directory.apache.org/apacheds/1.5/ ApacheDS] LDAP directory server.
 
This program drives the <tt>ldapsearch</tt> command and captures the output into an external data queue via ooRexx <tt>rxqueue</tt> facility. The contents of the queue are then read into program variables for further processing.
 
<syntaxhighlight lang="oorexx">/* Rexx */
do
LDAP_URL = 'ldap://localhost:11389'
LDAP_DN_STR = 'uid=admin,ou=system'
LDAP_CREDS = '********'
LDAP_BASE_DN = 'ou=users,o=mojo'
LDAP_SCOPE = 'sub'
LDAP_FILTER = '"(&(objectClass=person)(&(uid=*mil*)))"'
LDAP_ATTRIBUTES = '"dn" "cn" "sn" "uid"'
 
ldapCommand = ,
'ldapsearch' ,
'-s base' ,
'-H' LDAP_URL ,
'-LLL' ,
'-x' ,
'-v' ,
'-s' LDAP_SCOPE ,
'-D' LDAP_DN_STR ,
'-w' LDAP_CREDS ,
'-b' LDAP_BASE_DN ,
LDAP_FILTER ,
LDAP_ATTRIBUTES ,
'2>&1' ,
'|' ,
'rxqueue' ,
''
 
address command,
ldapCommand
 
ldapResult. = ''
loop ln = 1 to queued()
parse pull line
ldapResult.0 = ln
ldapResult.ln = line
end ln
 
loop ln = 1 to ldapResult.0
parse var ldapResult.ln 'dn:' dn_ ,
0 'uid:' uid_ ,
0 'sn:' sn_ ,
0 'cn:' cn_
select
when length(strip(dn_, 'b')) > 0 then dn = dn_
when length(strip(uid_, 'b')) > 0 then uid = uid_
when length(strip(sn_, 'b')) > 0 then sn = sn_
when length(strip(cn_, 'b')) > 0 then cn = cn_
otherwise nop
end
end ln
 
say 'Distiguished Name:' dn
say ' Common Name:' cn
say ' Surname:' sn
say ' userID:' uid
 
return
end
exit
</syntaxhighlight>
'''Output:'''
<pre>
Distiguished Name: cn=John Milton,ou=users,o=mojo
Common Name: John Milton
Surname: Milton
userID: jmilton
</pre>
 
=={{header|Perl}}==
{{Trans|Raku}}
<syntaxhighlight lang="perl"># 20210306 Perl programming solution
 
use strict;
use warnings;
 
use Net::LDAP;
 
my $ldap = Net::LDAP->new( 'ldap://ldap.forumsys.com' ) or die "$@";
 
my $mesg = $ldap->bind( "cn=read-only-admin,dc=example,dc=com",
password => "password" );
$mesg->code and die $mesg->error;
 
my $srch = $ldap->search( base => "dc=example,dc=com",
filter => "(|(uid=gauss))" );
 
$srch->code and die $srch->error;
 
foreach my $entry ($srch->entries) { $entry->dump }
 
$mesg = $ldap->unbind;</syntaxhighlight>
{{out}}
<pre>
------------------------------------------------------------------------
dn:uid=gauss,dc=example,dc=com
 
objectClass: inetOrgPerson
organizationalPerson
person
top
cn: Carl Friedrich Gauss
sn: Gauss
uid: gauss
mail: gauss@ldap.forumsys.com
</pre>
 
=={{header|Phix}}==
{{trans|C}}
<!--<syntaxhighlight lang="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>
<span style="color: #008080;">constant</span> <span style="color: #000000;">servers</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span>
<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: #008080;">if</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">=</span><span style="color: #000000;">LDAP_SUCCESS</span> <span style="color: #008080;">then</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">pMsg</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ldap_search_s</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ld</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"dc=somewhere,dc=com"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">LDAP_SCOPE_SUBTREE</span><span style="color: #0000FF;">,</span>
<span style="color: #000080;font-style:italic;">-- search for all persons whose names start with joe or shmoe</span>
<span style="color: #008000;">"(&(objectclass=person)(|(cn=joe*)(cn=shmoe*)))"</span><span style="color: #0000FF;">,</span>
<span style="color: #004600;">NULL</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- return all attributes</span>
<span style="color: #000000;">0</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- want both types and values of attrs
-- arduously do stuff here to result, with ldap_first_message(), ldap_parse_result(), etc.</span>
<span style="color: #000000;">ldap_msgfree</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pMsg</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- free messages (embedded NULL check)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</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}}
Note the code inside res=LDAP_SUCCESS has not been tested beyond compiling succesfully, see also
the comments in [[Active_Directory/Connect#Phix]]
<pre>
ldap.somewhere.com: 81 [LDAP_SERVER_DOWN]
</pre>
 
Line 338 ⟶ 500:
{{libheader|php-ldap}}
 
<langsyntaxhighlight lang="php"><?php
 
$l = ldap_connect('ldap.example.com');
Line 353 ⟶ 515:
$entries = ldap_get_entries($l, $search);
 
var_dump($entries);</langsyntaxhighlight>
 
=={{header|PicoLisp}}==
<syntaxhighlight lang="picolisp">(de ldapsearch (Sn)
(in
(list "ldapsearch" "-xH" "ldap://db.debian.org"
"-b" "dc=debian,dc=org"
(pack "sn=" Sn) )
(list
(cons 'cn (prog (from "cn: ") (line T)))
(cons 'uid (prog (from "uid: ") (line T))) ) ) )</syntaxhighlight>
Test:
<pre>: (ldapsearch "Fischer")
-> ((cn . "Mika") (uid . "mf"))</pre>
 
=={{header|PowerShell}}==
 
<syntaxhighlight lang="python">
Import-Module ActiveDirectory
 
$searchData = "user name"
$searchBase = "DC=example,DC=com"
 
#searches by some of the most common unique identifiers
get-aduser -Filter((DistinguishedName -eq $searchdata) -or (UserPrincipalName -eq $searchdata) -or (SamAccountName -eq $searchdata)) -SearchBase $searchBase
 
</syntaxhighlight>
 
=={{header|Python}}==
Line 361 ⟶ 549:
[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 379 ⟶ 567:
finally:
l.unbind()
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" line>
 
# 20190718 Raku programming solution
# https://github.com/perl6/doc/issues/2898
# https://www.facebook.com/groups/perl6/permalink/2379873082279037/
 
# Reference:
# https://github.com/Altai-man/cro-ldap
# https://www.forumsys.com/tutorials/integration-how-to/ldap/online-ldap-test-server/
 
use v6.d;
use Cro::LDAP::Client;
 
my $client = await Cro::LDAP::Client.connect('ldap://ldap.forumsys.com');
 
my $bind = await $client.bind(
name=>'cn=read-only-admin,dc=example,dc=com',password=>'password'
);
die $bind.error-message if $bind.result-code;
 
my $resp = $client.search(
:dn<dc=example,dc=com>, base=>"ou=mathematicians", filter=>'(&(uid=gauss))'
);
 
react {
whenever $resp -> $entry {
for $entry.attributes.kv -> $k, $v {
my $value-str = $v ~~ Blob ?? $v.decode !! $v.map(*.decode);
note "$k -> $value-str";
}
}
}</syntaxhighlight>
{{out}}
<pre>objectClass -> inetOrgPerson organizationalPerson top person
mail -> gauss@ldap.forumsys.com
uid -> gauss
cn -> Carl Friedrich Gauss
sn -> Gauss
</pre>
 
=={{header|REXX}}==
{{Works with|ooRexx}}
{{Works with|Regina}}
Using LDAP connecting to a local [http://directory.apache.org/apacheds/1.5/ ApacheDS] LDAP directory server.
 
A little contrived; this [[REXX]] program drives the <tt>ldapsearch</tt> command.
<syntaxhighlight lang="rexx">/* Rexx */
do
LDAP_URL = 'ldap://localhost:11389'
LDAP_DN_STR = 'uid=admin,ou=system'
LDAP_CREDS = '********'
LDAP_BASE_DN = 'ou=users,o=mojo'
LDAP_SCOPE = 'sub'
LDAP_FILTER = '"(&(objectClass=person)(&(uid=*mil*)))"'
LDAP_ATTRIBUTES = '"dn" "cn" "sn" "uid"'
 
ldapCommand = ,
'ldapsearch' ,
'-s base' ,
'-H' LDAP_URL ,
'-LLL' ,
'-x' ,
'-v' ,
'-s' LDAP_SCOPE ,
'-D' LDAP_DN_STR ,
'-w' LDAP_CREDS ,
'-b' LDAP_BASE_DN ,
LDAP_FILTER ,
LDAP_ATTRIBUTES ,
''
 
say ldapCommand
address command,
ldapCommand
 
return
end
exit
</syntaxhighlight>
'''Output:'''
<pre>
ldapsearch -s base -H ldap://localhost:11389 -LLL -x -v -s sub -D uid=admin,ou=system -w ******** -b ou=users,o=mojo "(&(objectClass=person)(&(uid=*mil*)))" "dn" "cn" "sn" "uid"
ldap_initialize( ldap://localhost:11389/??base )
filter: (&(objectClass=person)(&(uid=*mil*)))
requesting: dn cn sn uid
dn: cn=John Milton,ou=users,o=mojo
uid: jmilton
sn: Milton
cn: John Milton
 
</pre>
 
=={{header|Ruby}}==
Line 385 ⟶ 667:
 
{{libheader|RubyGems}}
<langsyntaxhighlight lang="ruby">require 'rubygems'
require 'net/ldap'
 
Line 398 ⟶ 680:
results = ldap.search(:filter => filter) # returns an array of Net::LDAP::Entry objects
 
puts results[0][:sn] # ==> "Jackman"</langsyntaxhighlight>
 
=={{header|Run BASIC}}==
Line 407 ⟶ 689:
[rename] files
[view] image files</pre>
<langsyntaxhighlight lang="runbasic">' ---------------------------------------------
' Directory maintenance
' ---------------------------------------------
Line 513 ⟶ 795:
WEND
END FUNCTION
end</langsyntaxhighlight>
Output as seen by the client on the web<pre>
Volume in drive C has no label.
Line 533 ⟶ 815:
5 File(s) 748,993,785 bytes
7 Dir(s) 952,183,820,288 bytes free</pre>
 
=={{header|Scala}}==
<syntaxhighlight lang="scala">import org.apache.directory.api.ldap.model.message.SearchScope
import org.apache.directory.ldap.client.api.{LdapConnection, LdapNetworkConnection}
 
object LdapSearchDemo extends App {
 
class LdapSearch {
 
def demonstrateSearch(): Unit = {
 
val conn = new LdapNetworkConnection("localhost", 11389)
try {
conn.bind("uid=admin,ou=system", "********")
search(conn, "*mil*")
conn.unBind()
} finally if (conn != null) conn.close()
 
}
 
private def search(connection: LdapConnection, uid: String): Unit = {
val baseDn = "ou=users,o=mojo"
val filter = "(&(objectClass=person)(&(uid=" + uid + ")))"
val scope = SearchScope.SUBTREE
val attributes = List("dn", "cn", "sn", "uid")
var ksearch = 0
val cursor = connection.search(baseDn, filter, scope, attributes: _*)
while (cursor.next) {
ksearch += 1
val entry = cursor.get
printf("Search entry %d = %s%n", ksearch, entry)
}
}
}
 
new LdapSearch().demonstrateSearch()
 
}</syntaxhighlight>
 
=={{header|Tcl}}==
One can do it with the low level [[Connect to Active Directory]] based handle with this code:
 
This is just the basic setup.
<syntaxhighlight lang="tcl">set Username "TestUser"
set Filter "((&objectClass=*)(sAMAccountName=$Username))"
set Base "dc=skycityauckland,dc=sceg,dc=com"
set Attrs distinguishedName</syntaxhighlight>
 
Now do the actual search.
<syntaxhighlight lang="tcl">set result [ldap::search $conn $Base $Filter $Attrs -scope subtree]</syntaxhighlight>
If we have only a single result its easy:
<syntaxhighlight lang="tcl">if {[llength $result] == 1} {
puts [dict get [lindex $result 0 1] distinguishedName]
}</syntaxhighlight>
 
Looping over the result set to output some values.
<syntaxhighlight lang="tcl">foreach pair $result {
lassign $pair cn attributes
puts [dict get $attributes distinguishedName]
}</syntaxhighlight>
 
If you're bored you can also use this instead:
<syntaxhighlight lang="tcl">package require ldapx
set conn [ldapx::connect $BindDN $Password]
$conn traverse $Base $Filter $Attrs e {
puts [$e get distinguishedName]
}</syntaxhighlight>
 
=={{header|UNIX Shell}}==
Using LDAP connecting to a local [http://directory.apache.org/apacheds/1.5/ ApacheDS] LDAP directory server.
 
A shell script to drive the <tt>ldapsearch</tt> command.
<langsyntaxhighlight lang="bash">#!/bin/sh
 
LDAP_HOST="localhost"
LDAP_PORT=11389
LDAP_DN_STR="uid=admin,ou=system"
LDAP_CREDS="secret********"
LDAP_BASE_DN="ou=users,o=mojo"
LDAP_SCOPE="sub"
Line 560 ⟶ 912:
$LDAP_FILTER \
$LDAP_ATTRIBUTES
</syntaxhighlight>
</lang>
'''Output:'''
<pre>
Line 572 ⟶ 924:
 
</pre>
 
=={{header|Tcl}}==
One can do it with the low level [[Connect to Active Directory]] based handle with this code:
 
This is just the basic setup.
<lang tcl>set Username "TestUser"
set Filter "((&objectClass=*)(sAMAccountName=$Username))"
set Base "dc=skycityauckland,dc=sceg,dc=com"
set Attrs distinguishedName</lang>
 
Now do the actual search.
<lang tcl>set result [ldap::search $conn $Base $Filter $Attrs -scope subtree]</lang>
If we have only a single result its easy:
<lang tcl>if {[llength $result] == 1} {
puts [dict get [lindex $result 0 1] distinguishedName]
}</lang>
 
Looping over the result set to output some values.
<lang tcl>foreach pair $result {
lassign $pair cn attributes
puts [dict get $attributes distinguishedName]
}</lang>
 
If you're bored you can also use this instead:
<lang tcl>package require ldapx
set conn [ldapx::connect $BindDN $Password]
$conn traverse $Base $Filter $Attrs e {
puts [$e get distinguishedName]
}</lang>
 
=={{header|VBScript}}==
The search string and execution of the string
<langsyntaxhighlight lang="vbscript">strUsername = "TestUser"
strQuery = "<LDAP://dc=skycityauckland,dc=sceg,dc=com>;"_
& "(&(objectclass=*)(samaccountname=" & strUsername & "));distinguishedname;subtree"
Line 611 ⟶ 933:
objCmd.Properties("Page Size")=100
objCmd.CommandText = strQuery
Set objRS = objCmd.Execute</langsyntaxhighlight>
 
Doing something with a single result (this will output the returned users full DN)
<langsyntaxhighlight lang="vbscript">If objRS.RecordCount = 1 Then
WScript.Echo objRS.Fields("DistinguishedName")
End If</langsyntaxhighlight>
 
Doing something with multiple results (this will output each returned users full DN)
<langsyntaxhighlight lang="vbscript">If objRS.RecordCount > 0 Then
For Each objUser in ObjRS
WScript.Echo objRS.Fields("DistinguishedName")
Next
End If</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.
 
Note that, in an actual case, one would need to wrap more LDAP functions to process the search results.
<syntaxhighlight lang="wren">/* Active_Directory_Search_for_a_user.wren */
 
var LDAP_SCOPE_SUBTREE = 0x0002
 
foreign class LDAPMessage {
construct new() {}
 
foreign msgfree()
}
 
foreign class LDAP {
construct init(host, port) {}
 
foreign simpleBindS(name, password)
 
foreign searchS(base, scope, filter, attrs, attrsOnly, res)
 
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)
 
var result = LDAPMessage.new()
ld.searchS(
"dc:somewhere,dc=com",
LDAP_SCOPE_SUBTREE,
"(&(objectclass=person)(|(cn=joe*)(cn=shmoe*)))", // all persons whose names start with joe or shmoe
[], 0, result
)
 
// do stuff with result
 
result.msgfree()
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_ldapMessageAllocate(WrenVM* vm) {
wrenSetSlotNewForeign(vm, 0, 0, sizeof(LDAPMessage*));
}
 
void C_msgfree(WrenVM* vm) {
LDAPMessage* msg = *(LDAPMessage**)wrenGetSlotForeign(vm, 0);
ldap_msgfree(msg);
}
 
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_searchS(WrenVM* vm) {
LDAP* ldap = *(LDAP**)wrenGetSlotForeign(vm, 0);
const char *base = wrenGetSlotString(vm, 1);
int scope = (int)(ber_int_t)wrenGetSlotDouble(vm, 2);
const char *filter = wrenGetSlotString(vm, 3);
// no need to get attrs from slot 4 as we want all of them
int attrsonly = (int)wrenGetSlotDouble(vm, 5);
LDAPMessage** res = (LDAPMessage**)wrenGetSlotForeign(vm, 6);
ldap_search_s(ldap, base, scope, filter, NULL, attrsonly, res);
}
 
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;
} else if (strcmp(className, "LDAPMessage") == 0) {
methods.allocate = C_ldapMessageAllocate;
}
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, "searchS(_,_,_,_,_,_)") == 0) return C_searchS;
if (!isStatic && strcmp(signature, "unbind()") == 0) return C_unbind;
} else if (strcmp(className, "LDAPMessage") == 0) {
if (!isStatic && strcmp(signature, "msgfree()") == 0) return C_msgfree;
} 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_Search_for_a_user.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|ACL2}}
{{omit from|AWK}}
{{omit from|Clojure}}
{{omit from|Inform 7|Does not have network access.}}
{{omit from|Lilypond}}
{{omit from|Lingo}}
{{omit from|M4}}
{{omit from|Mathematica}}
{{omit from|Maxima}}
{{omit from|ML/I}}
{{omit from|MIPS Assembly|None of the commonly used implementations can access AD functions}}
{{omit from|ML/I}}
{{omit from|PARI/GP}}
{{omit from|PostScript}}
{{omit from|TI-83 BASIC}} {{omit from|TI-89 BASIC}} <!-- Does not have network access. -->
{{omit from|Retro}}
{{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.}}
 
[[Category:Active Directory]]
2,122

edits