1#! /bin/sh
2# -*- tcl -*- \
3exec tclsh "$0" ${1+"$@"}
4
5#-----------------------------------------------------------------------------
6#   
7#   A small script to test the update/modify/delete capabilites of
8#   pure-Tcl LDAP package.
9#
10#   This has been used against OpenLDAP test suite 
11#   (pause at step 'test003-search' Waiting 5 secods for slapd to start ...'
12#
13#-----------------------------------------------------------------------------
14
15package require ldap
16#source ./ldap.tcl
17
18#-----------------------------------------------------------------------------
19#   Query
20#
21#-----------------------------------------------------------------------------
22
23proc Query {handle} {
24    set results [ldap::search $handle \
25	    "o=University of Michigan,c=US" \
26	    "(cn=Tes*)" {}]
27
28    foreach result $results {
29	foreach {object attributes} $result break
30
31	#------------------------------------------
32	#    calculate optimal width
33	#------------------------------------------
34	set width 0
35	set Attribs {}
36	foreach {type values} $attributes {
37	    if {[string length $type] > $width} {
38		set width [string length $type] 
39	    }
40	    lappend Attribs [list $type $values]
41	}     
42
43	puts "object='$object'"
44
45	foreach sortedAttrib  [lsort -index 0 $Attribs] {
46	    foreach {type values} $sortedAttrib break
47	    foreach value $values {
48		regsub -all "\[\x01-\x1f\]" $value ? value
49		puts [format "  %-${width}s %s" $type $value]
50	    }
51	}
52	puts ""
53    }
54}
55
56#-----------------------------------------------------------------------------
57#                begin of   M A I N  part
58#-----------------------------------------------------------------------------
59
60#---------------------------------------------------------------
61#   connect to the local LDAP server using a non standard port
62#   (here OpenLDAP test suite)
63#
64#---------------------------------------------------------------
65set handle [ldap::connect localhost 9009]
66
67#---------------------------------------------------------------
68#   bind to the manager user (which was update/insert rights)
69#   ie. login into LDAP server
70#
71#---------------------------------------------------------------
72set dn "cn=Manager, o=University of Michigan, c=US"
73set pw secret
74
75ldap::bind $handle $dn $pw
76
77#---------------------------------------------------------------
78#   create a new object (DN) with a couple of attrbitues
79#
80#---------------------------------------------------------------
81set dn "cn=Test User,ou=People,o=University of Michigan,c=US"
82
83ldap::add $handle $dn {
84
85    objectClass OpenLDAPperson
86    cn          "Test User"
87    mail        "test.user@google.com"
88    uid         "testuid"
89    sn          User
90}
91
92puts "after DN creation:"
93Query $handle
94
95#---------------------------------------------------------------
96#   replace some attributes (overwrite or create new one!)
97#
98#---------------------------------------------------------------
99ldap::modify $handle $dn [list drink icetea uid JOLO]
100
101puts "after replacing some attrbitues:"
102Query $handle
103
104#---------------------------------------------------------------
105#   add some attributes (even multiple times!)
106#
107#---------------------------------------------------------------
108ldap::modify $handle $dn {} {} [list drink water \
109	drink orangeJuice pager "+1 313 555 7671"]
110
111puts "after adding multiple attrbitues:"
112Query $handle
113
114#----------------------------------------------------------------
115#   delete some attributes ( delete the whole attribute or only
116#   matching ones)
117#
118#----------------------------------------------------------------
119ldap::modify $handle $dn {} [list drink water \
120	pager ""]
121
122puts "after delete some attrbitues:"
123Query $handle
124
125#----------------------------------------------------------------
126#   move object (DN) to different place in LDAP tree, 
127#   here: basically rename it
128#
129#----------------------------------------------------------------
130ldap::modifyDN $handle $dn "cn=Tester"
131
132puts "after moving/renaming DN:"
133Query $handle
134
135#---------------------------------------------------------------
136#   delete the whole object plus all its attrbutes
137#
138#---------------------------------------------------------------
139set dn "cn=Tester,ou=People,o=University of Michigan,c=US"
140ldap::delete $handle $dn 
141
142puts "after deleting the whole DN:"
143Query $handle
144
145#---------------------------------------------------------------
146#   unbind and disconnect from the LDAP server
147#
148#---------------------------------------------------------------
149ldap::unbind     $handle
150ldap::disconnect $handle
151
152#-----------------------------------------------------------------------------
153#                end of   M A I N  part
154#-----------------------------------------------------------------------------
155