Active Directory/Search for a user: Difference between revisions
Content added Content deleted
(Added Wren) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 4: | Line 4: | ||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang=C>#include <ldap.h> |
||
char *name, *password; |
char *name, *password; |
||
Line 24: | Line 24: | ||
ldap_msgfree(*result); /* free messages */ |
ldap_msgfree(*result); /* free messages */ |
||
ldap_unbind(ld); /* disconnect */</ |
ldap_unbind(ld); /* disconnect */</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
Based on dopenldap. |
Based on dopenldap. |
||
<syntaxhighlight lang=d> |
|||
<lang d> |
|||
import openldap; |
import openldap; |
||
import std.stdio; |
import std.stdio; |
||
Line 57: | Line 57: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Eiffel}}== |
=={{header|Eiffel}}== |
||
Line 63: | Line 63: | ||
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. |
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. |
||
< |
<syntaxhighlight lang=Eiffel> |
||
feature -- Validation |
feature -- Validation |
||
Line 76: | Line 76: | ||
Result := cwel_is_credential_valid (l_domain.item, l_username.item, l_password.item) |
Result := cwel_is_credential_valid (l_domain.item, l_username.item, l_password.item) |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
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: |
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: |
||
< |
<syntaxhighlight lang=Eiffel> |
||
cwel_is_credential_valid (a_domain, a_username, a_password: POINTER): BOOLEAN |
cwel_is_credential_valid (a_domain, a_username, a_password: POINTER): BOOLEAN |
||
external |
external |
||
Line 87: | Line 87: | ||
"return cwel_is_credential_valid ((LPTSTR) $a_domain, (LPTSTR) $a_username, (LPTSTR) $a_password);" |
"return cwel_is_credential_valid ((LPTSTR) $a_domain, (LPTSTR) $a_username, (LPTSTR) $a_password);" |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
Line 93: | Line 93: | ||
<br> |
<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. |
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 ( |
import ( |
||
Line 117: | Line 117: | ||
} |
} |
||
log.Printf("Groups: %+v", groups) |
log.Printf("Groups: %+v", groups) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
Line 123: | Line 123: | ||
Example uses the [https://hackage.haskell.org/package/ldap-client <tt>ldap-client</tt>] package: |
Example uses the [https://hackage.haskell.org/package/ldap-client <tt>ldap-client</tt>] package: |
||
< |
<syntaxhighlight lang=haskell>{-# LANGUAGE OverloadedStrings #-} |
||
module Main (main) where |
module Main (main) where |
||
Line 137: | Line 137: | ||
Ldap.search ldap (Ldap.Dn "o=example.com") (Ldap.typesOnly True) (Attr "uid" := Text.encodeUtf8 "user") [] |
Ldap.search ldap (Ldap.Dn "o=example.com") (Ldap.typesOnly True) (Attr "uid" := Text.encodeUtf8 "user") [] |
||
for_ entries $ \entry -> |
for_ entries $ \entry -> |
||
print entry</ |
print entry</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
Line 143: | Line 143: | ||
The following code uses the Apache Directory project, version 1.0.0. |
The following code uses the Apache Directory project, version 1.0.0. |
||
< |
<syntaxhighlight lang=java>import java.io.IOException; |
||
import org.apache.directory.api.ldap.model.cursor.CursorException; |
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.cursor.EntryCursor; |
||
Line 180: | Line 180: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang=julia>using LDAPClient |
||
function searchLDAPusers(searchstring, uname, pword, host=["example", "com"]) |
function searchLDAPusers(searchstring, uname, pword, host=["example", "com"]) |
||
Line 204: | Line 204: | ||
searchLDAPusers("Mario", "my-username", "my-password") |
searchLDAPusers("Mario", "my-username", "my-password") |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|NetRexx}}== |
=={{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. |
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. |
||
< |
<syntaxhighlight lang=NetRexx>/* NetRexx */ |
||
options replace format comments java crossref symbols binary |
options replace format comments java crossref symbols binary |
||
Line 327: | Line 327: | ||
return state |
return state |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Output:''' |
'''Output:''' |
||
<pre> |
<pre> |
||
Line 348: | Line 348: | ||
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. |
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 |
do |
||
LDAP_URL = 'ldap://localhost:11389' |
LDAP_URL = 'ldap://localhost:11389' |
||
Line 408: | Line 408: | ||
end |
end |
||
exit |
exit |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Output:''' |
'''Output:''' |
||
<pre> |
<pre> |
||
Line 419: | Line 419: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{Trans|Raku}} |
{{Trans|Raku}} |
||
< |
<syntaxhighlight lang=perl># 20210306 Perl programming solution |
||
use strict; |
use strict; |
||
Line 440: | Line 440: | ||
foreach my $entry ($srch->entries) { $entry->dump } |
foreach my $entry ($srch->entries) { $entry->dump } |
||
$mesg = $ldap->unbind;</ |
$mesg = $ldap->unbind;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 458: | Line 458: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{trans|C}} |
{{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;">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 484: | Line 484: | ||
<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: #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> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
Note the code inside res=LDAP_SUCCESS has not been tested beyond compiling succesfully, see also |
Note the code inside res=LDAP_SUCCESS has not been tested beyond compiling succesfully, see also |
||
Line 496: | Line 496: | ||
{{libheader|php-ldap}} |
{{libheader|php-ldap}} |
||
< |
<syntaxhighlight lang=php><?php |
||
$l = ldap_connect('ldap.example.com'); |
$l = ldap_connect('ldap.example.com'); |
||
Line 511: | Line 511: | ||
$entries = ldap_get_entries($l, $search); |
$entries = ldap_get_entries($l, $search); |
||
var_dump($entries);</ |
var_dump($entries);</syntaxhighlight> |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang=PicoLisp>(de ldapsearch (Sn) |
||
(in |
(in |
||
(list "ldapsearch" "-xH" "ldap://db.debian.org" |
(list "ldapsearch" "-xH" "ldap://db.debian.org" |
||
Line 521: | Line 521: | ||
(list |
(list |
||
(cons 'cn (prog (from "cn: ") (line T))) |
(cons 'cn (prog (from "cn: ") (line T))) |
||
(cons 'uid (prog (from "uid: ") (line T))) ) ) )</ |
(cons 'uid (prog (from "uid: ") (line T))) ) ) )</syntaxhighlight> |
||
Test: |
Test: |
||
<pre>: (ldapsearch "Fischer") |
<pre>: (ldapsearch "Fischer") |
||
Line 528: | Line 528: | ||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
< |
<syntaxhighlight lang=python> |
||
Import-Module ActiveDirectory |
Import-Module ActiveDirectory |
||
Line 537: | Line 537: | ||
get-aduser -Filter((DistinguishedName -eq $searchdata) -or (UserPrincipalName -eq $searchdata) -or (SamAccountName -eq $searchdata)) -SearchBase $searchBase |
get-aduser -Filter((DistinguishedName -eq $searchdata) -or (UserPrincipalName -eq $searchdata) -or (SamAccountName -eq $searchdata)) -SearchBase $searchBase |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
Line 545: | Line 545: | ||
[http://www.python-ldap.org/doc/html/index.html python-ldap Documentation] |
[http://www.python-ldap.org/doc/html/index.html python-ldap Documentation] |
||
< |
<syntaxhighlight lang=python>import ldap |
||
l = ldap.initialize("ldap://ldap.example.com") |
l = ldap.initialize("ldap://ldap.example.com") |
||
Line 563: | Line 563: | ||
finally: |
finally: |
||
l.unbind() |
l.unbind() |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
< |
<syntaxhighlight lang=perl6> |
||
# 20190718 Raku programming solution |
# 20190718 Raku programming solution |
||
Line 598: | Line 598: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>objectClass -> inetOrgPerson organizationalPerson top person |
<pre>objectClass -> inetOrgPerson organizationalPerson top person |
||
Line 613: | Line 613: | ||
A little contrived; this [[REXX]] program drives the <tt>ldapsearch</tt> command. |
A little contrived; this [[REXX]] program drives the <tt>ldapsearch</tt> command. |
||
< |
<syntaxhighlight lang=REXX>/* Rexx */ |
||
do |
do |
||
LDAP_URL = 'ldap://localhost:11389' |
LDAP_URL = 'ldap://localhost:11389' |
||
Line 645: | Line 645: | ||
end |
end |
||
exit |
exit |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Output:''' |
'''Output:''' |
||
<pre> |
<pre> |
||
Line 663: | Line 663: | ||
{{libheader|RubyGems}} |
{{libheader|RubyGems}} |
||
< |
<syntaxhighlight lang=ruby>require 'rubygems' |
||
require 'net/ldap' |
require 'net/ldap' |
||
Line 676: | Line 676: | ||
results = ldap.search(:filter => filter) # returns an array of Net::LDAP::Entry objects |
results = ldap.search(:filter => filter) # returns an array of Net::LDAP::Entry objects |
||
puts results[0][:sn] # ==> "Jackman"</ |
puts results[0][:sn] # ==> "Jackman"</syntaxhighlight> |
||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
Line 685: | Line 685: | ||
[rename] files |
[rename] files |
||
[view] image files</pre> |
[view] image files</pre> |
||
< |
<syntaxhighlight lang=runbasic>' --------------------------------------------- |
||
' Directory maintenance |
' Directory maintenance |
||
' --------------------------------------------- |
' --------------------------------------------- |
||
Line 791: | Line 791: | ||
WEND |
WEND |
||
END FUNCTION |
END FUNCTION |
||
end</ |
end</syntaxhighlight> |
||
Output as seen by the client on the web<pre> |
Output as seen by the client on the web<pre> |
||
Volume in drive C has no label. |
Volume in drive C has no label. |
||
Line 813: | Line 813: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang=Scala>import org.apache.directory.api.ldap.model.message.SearchScope |
||
import org.apache.directory.ldap.client.api.{LdapConnection, LdapNetworkConnection} |
import org.apache.directory.ldap.client.api.{LdapConnection, LdapNetworkConnection} |
||
Line 848: | Line 848: | ||
new LdapSearch().demonstrateSearch() |
new LdapSearch().demonstrateSearch() |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
Line 854: | Line 854: | ||
This is just the basic setup. |
This is just the basic setup. |
||
< |
<syntaxhighlight lang=tcl>set Username "TestUser" |
||
set Filter "((&objectClass=*)(sAMAccountName=$Username))" |
set Filter "((&objectClass=*)(sAMAccountName=$Username))" |
||
set Base "dc=skycityauckland,dc=sceg,dc=com" |
set Base "dc=skycityauckland,dc=sceg,dc=com" |
||
set Attrs distinguishedName</ |
set Attrs distinguishedName</syntaxhighlight> |
||
Now do the actual search. |
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: |
If we have only a single result its easy: |
||
< |
<syntaxhighlight lang=tcl>if {[llength $result] == 1} { |
||
puts [dict get [lindex $result 0 1] distinguishedName] |
puts [dict get [lindex $result 0 1] distinguishedName] |
||
}</ |
}</syntaxhighlight> |
||
Looping over the result set to output some values. |
Looping over the result set to output some values. |
||
< |
<syntaxhighlight lang=tcl>foreach pair $result { |
||
lassign $pair cn attributes |
lassign $pair cn attributes |
||
puts [dict get $attributes distinguishedName] |
puts [dict get $attributes distinguishedName] |
||
}</ |
}</syntaxhighlight> |
||
If you're bored you can also use this instead: |
If you're bored you can also use this instead: |
||
< |
<syntaxhighlight lang=tcl>package require ldapx |
||
set conn [ldapx::connect $BindDN $Password] |
set conn [ldapx::connect $BindDN $Password] |
||
$conn traverse $Base $Filter $Attrs e { |
$conn traverse $Base $Filter $Attrs e { |
||
puts [$e get distinguishedName] |
puts [$e get distinguishedName] |
||
}</ |
}</syntaxhighlight> |
||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
Line 884: | Line 884: | ||
A shell script to drive the <tt>ldapsearch</tt> command. |
A shell script to drive the <tt>ldapsearch</tt> command. |
||
< |
<syntaxhighlight lang=bash>#!/bin/sh |
||
LDAP_HOST="localhost" |
LDAP_HOST="localhost" |
||
Line 908: | Line 908: | ||
$LDAP_FILTER \ |
$LDAP_FILTER \ |
||
$LDAP_ATTRIBUTES |
$LDAP_ATTRIBUTES |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Output:''' |
'''Output:''' |
||
<pre> |
<pre> |
||
Line 923: | Line 923: | ||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
The search string and execution of the string |
The search string and execution of the string |
||
< |
<syntaxhighlight lang=vbscript>strUsername = "TestUser" |
||
strQuery = "<LDAP://dc=skycityauckland,dc=sceg,dc=com>;"_ |
strQuery = "<LDAP://dc=skycityauckland,dc=sceg,dc=com>;"_ |
||
& "(&(objectclass=*)(samaccountname=" & strUsername & "));distinguishedname;subtree" |
& "(&(objectclass=*)(samaccountname=" & strUsername & "));distinguishedname;subtree" |
||
Line 929: | Line 929: | ||
objCmd.Properties("Page Size")=100 |
objCmd.Properties("Page Size")=100 |
||
objCmd.CommandText = strQuery |
objCmd.CommandText = strQuery |
||
Set objRS = objCmd.Execute</ |
Set objRS = objCmd.Execute</syntaxhighlight> |
||
Doing something with a single result (this will output the returned users full DN) |
Doing something with a single result (this will output the returned users full DN) |
||
< |
<syntaxhighlight lang=vbscript>If objRS.RecordCount = 1 Then |
||
WScript.Echo objRS.Fields("DistinguishedName") |
WScript.Echo objRS.Fields("DistinguishedName") |
||
End If</ |
End If</syntaxhighlight> |
||
Doing something with multiple results (this will output each returned users full DN) |
Doing something with multiple results (this will output each returned users full DN) |
||
< |
<syntaxhighlight lang=vbscript>If objRS.RecordCount > 0 Then |
||
For Each objUser in ObjRS |
For Each objUser in ObjRS |
||
WScript.Echo objRS.Fields("DistinguishedName") |
WScript.Echo objRS.Fields("DistinguishedName") |
||
Next |
Next |
||
End If</ |
End If</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
Line 949: | Line 949: | ||
Note that, in an actual case, one would need to wrap more LDAP functions to process the search results. |
Note that, in an actual case, one would need to wrap more LDAP functions to process the search results. |
||
< |
<syntaxhighlight lang=ecmascript>/* active_directory_search_for_user.wren */ |
||
var LDAP_SCOPE_SUBTREE = 0x0002 |
var LDAP_SCOPE_SUBTREE = 0x0002 |
||
Line 999: | Line 999: | ||
result.msgfree() |
result.msgfree() |
||
ld.unbind()</ |
ld.unbind()</syntaxhighlight> |
||
<br> |
<br> |
||
We now embed this in the following C program, compile and run it. |
We now embed this in the following C program, compile and run it. |
||
< |
<syntaxhighlight lang=c>#include <stdio.h> |
||
#include <stdio_ext.h> |
#include <stdio_ext.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
Line 1,145: | Line 1,145: | ||
free(script); |
free(script); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{omit from|ACL2}} |
{{omit from|ACL2}} |