Active Directory/Search for a user: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 83: Line 83:


puts results[0][:sn] # ==> "Jackman"</lang>
puts results[0][:sn] # ==> "Jackman"</lang>

=={{header|Run BASIC}}==
<pre>This allows the client on the web to see their directory.
The user can click on any file or directory and this will give them the following options:
[upload] data from their computer to the server
[delete] data from their directory
[rename] files
[view] image files</pre>
<lang runbasic>' ---------------------------------------------
' Directory maintenance
' ---------------------------------------------
cr$ = chr$(13)
dirOf$ = "c:\*.*" ' get directory of

' -------------------------------------------
' Shell out directory
' -------------------------------------------

[dirShell]
cls
html "<table bgcolor=lightsteelblue><TR><TD id=wk></TD></TABLE>"
loc$ = strRep$(dirOf$,"*.*","")
x$ = shell$("dir ";dirOf$)

i = 1
while word$(x$,i,cr$) <> ""
a$ = word$(x$,i,cr$)
if trim$(a$) = "" then goto [next]
if left$(a$,1) = " " then goto [next]
if left$(a$,1) = cr$ then goto [next]
type$ = mid$(a$,26,3)
size$ = mid$(a$,30,9)
size$ = strRep$(size$,",","")
size = val(size$)
if type$ <> "DIR" and size = 0 then goto [next]
name$ = mid$(a$,40)
a$ = strRep$(a$,"<","[")
a$ = strRep$(a$,">","]")
html left$(a$,39)
link #ddir,name$, [doDir]
#ddir setkey(type$;"|";loc$;name$)
html "<BR>"
goto [next1]
[next]
print a$
[next1]
i = i + 1
wend
wait
[doDir]
type$ = word$(EventKey$,1,"|")
name$ = word$(EventKey$,2,"|")

if type$ = "DIR" then
dirOf$ = name$;"\*.*"
goto [dirShell]
end if

html "<script> document.getElementById('wk').innerHTML = '"
nname$ = strRep$(name$,"\","\\")
html "What do you want to do with ";nname$;"<BR>"
button #dofile, "Upload",[upload]
button #dofile, "Delete",[delete]
button #rename, "Rename",[rename]
button #view, "View", [view]
html "';</script>"
wait

[delete]
kill name$
goto [dirShell]

[view]
nname$ = strRep$(name$,"\","/")
print "File:";nname$
nname$ = mid$(nname$,3)
html "<EMBED SRC=""..";nname$;""">"
print "<EMBED SRC=""..";nname$;""">"

wait

[upload]
print "Upload File:";name$
files #f, name$
if #f HASANSWER() = 0 then
print "File: ";name$;" not found"
end if

' -------------------------------------
' load data to directory
' -------------------------------------
OPEN name$ FOR binary AS #f
filedata$ = input$(#f, LOF(#f))
CLOSE #f
print filedata$
wait

f$ = photoDir$;uploadId$
OPEN f$ FOR binary AS #f
PRINT #f, filedata$
CLOSE #f
wait

' --------------------------------
' string replace rep str with
' --------------------------------
FUNCTION strRep$(strRep$,rep$,with$)
ln = len(rep$)
k = instr(strRep$,rep$)
while k
strRep$ = left$(strRep$,k - 1) + with$ + mid$(strRep$,k + ln)
k = instr(strRep$,rep$)
WEND
END FUNCTION
end</lang>
Output as seen by the client on the web
<pre> Volume in drive C has no label.
Volume Serial Number is F42C-D87A

Directory of c:\
06/10/2009 02:42 PM 24 autoexec.bat
06/10/2009 02:42 PM 10 config.sys
01/30/2012 02:26 PM 206 csb.log
03/09/2012 10:00 AM [DIR] data
02/07/2012 07:48 AM 748,990,464 precise-desktop-i386.iso
03/20/2012 04:07 PM [DIR] Program Files
02/05/2012 05:09 PM [DIR] Python
03/19/2012 04:55 PM [DIR] rbp101
01/30/2012 02:26 PM 3,081 RHDSetup.log
01/30/2012 10:14 PM [DIR] Users
01/30/2012 02:35 PM [DIR] wamp
03/06/2012 04:00 AM [DIR] Windows
5 File(s) 748,993,785 bytes
7 Dir(s) 952,183,820,288 bytes free</pre>


=={{header|Tcl}}==
=={{header|Tcl}}==

Revision as of 11:43, 21 March 2012

Task
Active Directory/Search for a user
You are encouraged to solve this task according to the task description, using any language you may know.

Make sure you Connect to Active Directory

C

<lang C>#include <ldap.h>

char *name, *password; ...

LDAP *ld = ldap_init("ldap.somewhere.com", 389); ldap_simple_bind_s(ld, name, password);

LDAPMessage **result; ldap_search_s(ld, "dc=somewhere,dc=com", LDAP_SCOPE_SUBTREE, /* search for all persons whose names start with joe or shmoe */ "(&(objectclass=person)(|(cn=joe*)(cn=shmoe*)))", NULL, /* return all attributes */ 0, /* want both types and values of attrs */ result); /* ldap will allocate room for return messages */

/* arduously do stuff here to result, with ldap_first_message(), ldap_parse_result(), etc. */

ldap_msgfree(*result); /* free messages */ ldap_unbind(ld); /* disconnect */</lang>

PicoLisp

<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))) ) ) )</lang>

Test:

: (ldapsearch "Fischer")
-> ((cn . "Mika") (uid . "mf"))

Python

Works with: Python version 2.6
Library: python-ldap

python-ldap Documentation

<lang python>import ldap

l = ldap.initialize("ldap://ldap.example.com") try:

   l.protocol_version = ldap.VERSION3
   l.set_option(ldap.OPT_REFERRALS, 0)
   bind = l.simple_bind_s("me@example.com", "password")
   
   base = "dc=example, dc=com"
   criteria = "(&(objectClass=user)(sAMAccountName=username))"
   attributes = ['displayName', 'company']
   result = l.search_s(base, ldap.SCOPE_SUBTREE, criteria, attributes)
   results = [entry for dn, entry in result if isinstance(entry, dict)]
   print results

finally:

   l.unbind()

</lang>

Ruby

Assume AD server talks LDAP.

Library: RubyGems

<lang ruby>require 'rubygems' require 'net/ldap'

ldap = Net::LDAP.new(:host => 'hostname', :base => 'base') ldap.authenticate('bind_dn', 'bind_pass')

filter = Net::LDAP::Filter.pres('objectclass') filter &= Net::LDAP::Filter.eq('sn','Jackman')

  1. or

filter = Net::LDAP::Filter.construct('(&(objectclass=*)(sn=Jackman))')

results = ldap.search(:filter => filter) # returns an array of Net::LDAP::Entry objects

puts results[0][:sn] # ==> "Jackman"</lang>

Run BASIC

This allows the client on the web to see their directory.
The user can click on any file or directory and this will give them the following options:
 [upload] data from their computer to the server
 [delete] data from their directory 
 [rename] files
 [view]   image files

<lang runbasic>' --------------------------------------------- ' Directory maintenance ' --------------------------------------------- cr$ = chr$(13) dirOf$ = "c:\*.*" ' get directory of

' ------------------------------------------- ' Shell out directory ' -------------------------------------------

[dirShell] cls

html "

"

loc$ = strRep$(dirOf$,"*.*","") x$ = shell$("dir ";dirOf$)

i = 1 while word$(x$,i,cr$) <> "" a$ = word$(x$,i,cr$) if trim$(a$) = "" then goto [next] if left$(a$,1) = " " then goto [next] if left$(a$,1) = cr$ then goto [next] type$ = mid$(a$,26,3) size$ = mid$(a$,30,9) size$ = strRep$(size$,",","") size = val(size$) if type$ <> "DIR" and size = 0 then goto [next] name$ = mid$(a$,40) a$ = strRep$(a$,"<","[") a$ = strRep$(a$,">","]") html left$(a$,39) link #ddir,name$, [doDir] #ddir setkey(type$;"|";loc$;name$) html "
" goto [next1]

    [next]

print a$

    [next1]

i = i + 1 wend wait [doDir] type$ = word$(EventKey$,1,"|") name$ = word$(EventKey$,2,"|")

if type$ = "DIR" then dirOf$ = name$;"\*.*" goto [dirShell] end if

html "<script> document.getElementById('wk').innerHTML = '" nname$ = strRep$(name$,"\","\\") html "What do you want to do with ";nname$;"
" button #dofile, "Upload",[upload] button #dofile, "Delete",[delete] button #rename, "Rename",[rename] button #view, "View", [view] html "';</script>" wait

[delete]

kill name$
goto [dirShell]

[view] nname$ = strRep$(name$,"\","/") print "File:";nname$ nname$ = mid$(nname$,3) html "<EMBED SRC=""..";nname$;""">" print "<EMBED SRC=""..";nname$;""">"

wait

[upload] print "Upload File:";name$ files #f, name$ if #f HASANSWER() = 0 then print "File: ";name$;" not found" end if

' ------------------------------------- ' load data to directory ' ------------------------------------- OPEN name$ FOR binary AS #f filedata$ = input$(#f, LOF(#f)) CLOSE #f print filedata$ wait

f$ = photoDir$;uploadId$ OPEN f$ FOR binary AS #f PRINT #f, filedata$ CLOSE #f wait

' -------------------------------- ' string replace rep str with ' -------------------------------- FUNCTION strRep$(strRep$,rep$,with$) ln = len(rep$) k = instr(strRep$,rep$) while k strRep$ = left$(strRep$,k - 1) + with$ + mid$(strRep$,k + ln) k = instr(strRep$,rep$) WEND END FUNCTION end</lang> Output as seen by the client on the web

 Volume in drive C has no label.
 Volume Serial Number is F42C-D87A

 Directory of c:\
06/10/2009  02:42 PM                24 autoexec.bat
06/10/2009  02:42 PM                10 config.sys
01/30/2012  02:26 PM               206 csb.log
03/09/2012  10:00 AM    [DIR]          data
02/07/2012  07:48 AM       748,990,464 precise-desktop-i386.iso
03/20/2012  04:07 PM    [DIR]          Program Files
02/05/2012  05:09 PM    [DIR]          Python
03/19/2012  04:55 PM    [DIR]          rbp101
01/30/2012  02:26 PM             3,081 RHDSetup.log
01/30/2012  10:14 PM    [DIR]          Users
01/30/2012  02:35 PM    [DIR]          wamp
03/06/2012  04:00 AM    [DIR]          Windows
               5 File(s)    748,993,785 bytes
               7 Dir(s) 952,183,820,288 bytes free

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>

VBScript

The search string and execution of the string <lang vbscript>strUsername = "TestUser" strQuery = "<LDAP://dc=skycityauckland,dc=sceg,dc=com>;"_

& "(&(objectclass=*)(samaccountname=" & strUsername & "));distinguishedname;subtree"

objCmd.ActiveConnection = objConn objCmd.Properties("Page Size")=100 objCmd.CommandText = strQuery Set objRS = objCmd.Execute</lang>

Doing something with a single result (this will output the returned users full DN) <lang vbscript>If objRS.RecordCount = 1 Then

 WScript.Echo objRS.Fields("DistinguishedName")

End If</lang>

Doing something with multiple results (this will output each returned users full DN) <lang vbscript>If objRS.RecordCount > 0 Then

 For Each objUser in ObjRS
   WScript.Echo objRS.Fields("DistinguishedName")
 Next

End If</lang>