Active Directory/Search for a user: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
Thundergnat (talk | contribs) m (Automated syntax highlighting fixup (second round - minor fixes)) |
||
Line 1: | Line 1: | ||
⚫ | |||
{{task|Programming environment operations}} |
{{task|Programming environment operations}} |
||
Line 4: | Line 5: | ||
=={{header|C}}== |
=={{header|C}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="c">#include <ldap.h> |
||
char *name, *password; |
char *name, *password; |
||
Line 28: | Line 29: | ||
=={{header|D}}== |
=={{header|D}}== |
||
Based on dopenldap. |
Based on dopenldap. |
||
<syntaxhighlight lang=d> |
<syntaxhighlight lang="d"> |
||
import openldap; |
import openldap; |
||
import std.stdio; |
import std.stdio; |
||
Line 63: | Line 64: | ||
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= |
<syntaxhighlight lang="eiffel"> |
||
feature -- Validation |
feature -- Validation |
||
Line 80: | Line 81: | ||
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= |
<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 93: | Line 94: | ||
<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 |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 123: | Line 124: | ||
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 #-} |
<syntaxhighlight lang="haskell">{-# LANGUAGE OverloadedStrings #-} |
||
module Main (main) where |
module Main (main) where |
||
Line 143: | Line 144: | ||
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; |
<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 183: | Line 184: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
<syntaxhighlight lang=julia>using LDAPClient |
<syntaxhighlight lang="julia">using LDAPClient |
||
function searchLDAPusers(searchstring, uname, pword, host=["example", "com"]) |
function searchLDAPusers(searchstring, uname, pword, host=["example", "com"]) |
||
Line 208: | Line 209: | ||
=={{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= |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref symbols binary |
options replace format comments java crossref symbols binary |
||
Line 348: | Line 349: | ||
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= |
<syntaxhighlight lang="oorexx">/* Rexx */ |
||
do |
do |
||
LDAP_URL = 'ldap://localhost:11389' |
LDAP_URL = 'ldap://localhost:11389' |
||
Line 419: | Line 420: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{Trans|Raku}} |
{{Trans|Raku}} |
||
<syntaxhighlight lang=perl># 20210306 Perl programming solution |
<syntaxhighlight lang="perl"># 20210306 Perl programming solution |
||
use strict; |
use strict; |
||
Line 458: | Line 459: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{trans|C}} |
{{trans|C}} |
||
<!--<syntaxhighlight lang= |
<!--<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 496: | Line 497: | ||
{{libheader|php-ldap}} |
{{libheader|php-ldap}} |
||
<syntaxhighlight lang=php><?php |
<syntaxhighlight lang="php"><?php |
||
$l = ldap_connect('ldap.example.com'); |
$l = ldap_connect('ldap.example.com'); |
||
Line 514: | Line 515: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="picolisp">(de ldapsearch (Sn) |
||
(in |
(in |
||
(list "ldapsearch" "-xH" "ldap://db.debian.org" |
(list "ldapsearch" "-xH" "ldap://db.debian.org" |
||
Line 528: | Line 529: | ||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
<syntaxhighlight lang=python> |
<syntaxhighlight lang="python"> |
||
Import-Module ActiveDirectory |
Import-Module ActiveDirectory |
||
Line 545: | Line 546: | ||
[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 |
<syntaxhighlight lang="python">import ldap |
||
l = ldap.initialize("ldap://ldap.example.com") |
l = ldap.initialize("ldap://ldap.example.com") |
||
Line 567: | Line 568: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="raku" line> |
||
# 20190718 Raku programming solution |
# 20190718 Raku programming solution |
||
Line 613: | Line 614: | ||
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= |
<syntaxhighlight lang="rexx">/* Rexx */ |
||
do |
do |
||
LDAP_URL = 'ldap://localhost:11389' |
LDAP_URL = 'ldap://localhost:11389' |
||
Line 663: | Line 664: | ||
{{libheader|RubyGems}} |
{{libheader|RubyGems}} |
||
<syntaxhighlight lang=ruby>require 'rubygems' |
<syntaxhighlight lang="ruby">require 'rubygems' |
||
require 'net/ldap' |
require 'net/ldap' |
||
Line 685: | Line 686: | ||
[rename] files |
[rename] files |
||
[view] image files</pre> |
[view] image files</pre> |
||
<syntaxhighlight lang=runbasic>' --------------------------------------------- |
<syntaxhighlight lang="runbasic">' --------------------------------------------- |
||
' Directory maintenance |
' Directory maintenance |
||
' --------------------------------------------- |
' --------------------------------------------- |
||
Line 813: | Line 814: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
<syntaxhighlight lang= |
<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 854: | Line 855: | ||
This is just the basic setup. |
This is just the basic setup. |
||
<syntaxhighlight lang=tcl>set Username "TestUser" |
<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" |
||
Line 860: | Line 861: | ||
Now do the actual search. |
Now do the actual search. |
||
<syntaxhighlight lang=tcl>set result [ldap::search $conn $Base $Filter $Attrs -scope subtree]</syntaxhighlight> |
<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} { |
<syntaxhighlight lang="tcl">if {[llength $result] == 1} { |
||
puts [dict get [lindex $result 0 1] distinguishedName] |
puts [dict get [lindex $result 0 1] distinguishedName] |
||
}</syntaxhighlight> |
}</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 { |
<syntaxhighlight lang="tcl">foreach pair $result { |
||
lassign $pair cn attributes |
lassign $pair cn attributes |
||
puts [dict get $attributes distinguishedName] |
puts [dict get $attributes distinguishedName] |
||
Line 874: | Line 875: | ||
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 |
<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 { |
||
Line 884: | Line 885: | ||
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 |
<syntaxhighlight lang="bash">#!/bin/sh |
||
LDAP_HOST="localhost" |
LDAP_HOST="localhost" |
||
Line 923: | Line 924: | ||
=={{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" |
<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 932: | Line 933: | ||
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 |
<syntaxhighlight lang="vbscript">If objRS.RecordCount = 1 Then |
||
WScript.Echo objRS.Fields("DistinguishedName") |
WScript.Echo objRS.Fields("DistinguishedName") |
||
End If</syntaxhighlight> |
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 |
<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") |
||
Line 949: | Line 950: | ||
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 */ |
<syntaxhighlight lang="ecmascript">/* active_directory_search_for_user.wren */ |
||
var LDAP_SCOPE_SUBTREE = 0x0002 |
var LDAP_SCOPE_SUBTREE = 0x0002 |
||
Line 1,002: | Line 1,003: | ||
<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> |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdio_ext.h> |
#include <stdio_ext.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
Line 1,147: | Line 1,148: | ||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
{{omit from|ACL2}} |
{{omit from|ACL2}} |
||
{{omit from|AWK}} |
{{omit from|AWK}} |
||
Line 1,156: | Line 1,158: | ||
{{omit from|Mathematica}} |
{{omit from|Mathematica}} |
||
{{omit from|Maxima}} |
{{omit from|Maxima}} |
||
⚫ | |||
{{omit from|ML/I}} |
{{omit from|ML/I}} |
||
⚫ | |||
{{omit from|PARI/GP}} |
{{omit from|PARI/GP}} |
||
{{omit from|PostScript}} |
{{omit from|PostScript}} |
||
⚫ | |||
{{omit from|Retro}} |
{{omit from|Retro}} |
||
{{omit from|TI-83 BASIC}} |
|||
⚫ | |||
{{omit from|Yorick|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|ZX Spectrum Basic|Does not have network access.}} |
||
⚫ |