File: ldapChangeTest

package info (click to toggle)
tcllib 1.20%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 68,064 kB
  • sloc: tcl: 216,842; ansic: 14,250; sh: 2,846; xml: 1,766; yacc: 1,145; pascal: 881; makefile: 107; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (152 lines) | stat: -rwxr-xr-x 4,704 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env tclsh
## -*- tcl -*-
#-----------------------------------------------------------------------------
#   
#   A small script to test the update/modify/delete capabilites of
#   pure-Tcl LDAP package.
#
#   This has been used against OpenLDAP test suite 
#   (pause at step 'test003-search' Waiting 5 secods for slapd to start ...'
#
#-----------------------------------------------------------------------------

package require ldap
#source ./ldap.tcl

#-----------------------------------------------------------------------------
#   Query
#
#-----------------------------------------------------------------------------

proc Query {handle} {
    set results [ldap::search $handle \
	    "o=University of Michigan,c=US" \
	    "(cn=Tes*)" {}]

    foreach result $results {
	foreach {object attributes} $result break

	#------------------------------------------
	#    calculate optimal width
	#------------------------------------------
	set width 0
	set Attribs {}
	foreach {type values} $attributes {
	    if {[string length $type] > $width} {
		set width [string length $type] 
	    }
	    lappend Attribs [list $type $values]
	}     

	puts "object='$object'"

	foreach sortedAttrib  [lsort -index 0 $Attribs] {
	    foreach {type values} $sortedAttrib break
	    foreach value $values {
		regsub -all "\[\x01-\x1f\]" $value ? value
		puts [format "  %-${width}s %s" $type $value]
	    }
	}
	puts ""
    }
}

#-----------------------------------------------------------------------------
#                begin of   M A I N  part
#-----------------------------------------------------------------------------

#---------------------------------------------------------------
#   connect to the local LDAP server using a non standard port
#   (here OpenLDAP test suite)
#
#---------------------------------------------------------------
set handle [ldap::connect localhost 9009]

#---------------------------------------------------------------
#   bind to the manager user (which was update/insert rights)
#   ie. login into LDAP server
#
#---------------------------------------------------------------
set dn "cn=Manager, o=University of Michigan, c=US"
set pw secret

ldap::bind $handle $dn $pw

#---------------------------------------------------------------
#   create a new object (DN) with a couple of attrbitues
#
#---------------------------------------------------------------
set dn "cn=Test User,ou=People,o=University of Michigan,c=US"

ldap::add $handle $dn {

    objectClass OpenLDAPperson
    cn          "Test User"
    mail        "test.user@google.com"
    uid         "testuid"
    sn          User
}

puts "after DN creation:"
Query $handle

#---------------------------------------------------------------
#   replace some attributes (overwrite or create new one!)
#
#---------------------------------------------------------------
ldap::modify $handle $dn [list drink icetea uid JOLO]

puts "after replacing some attrbitues:"
Query $handle

#---------------------------------------------------------------
#   add some attributes (even multiple times!)
#
#---------------------------------------------------------------
ldap::modify $handle $dn {} {} [list drink water \
	drink orangeJuice pager "+1 313 555 7671"]

puts "after adding multiple attrbitues:"
Query $handle

#----------------------------------------------------------------
#   delete some attributes ( delete the whole attribute or only
#   matching ones)
#
#----------------------------------------------------------------
ldap::modify $handle $dn {} [list drink water \
	pager ""]

puts "after delete some attrbitues:"
Query $handle

#----------------------------------------------------------------
#   move object (DN) to different place in LDAP tree, 
#   here: basically rename it
#
#----------------------------------------------------------------
ldap::modifyDN $handle $dn "cn=Tester"

puts "after moving/renaming DN:"
Query $handle

#---------------------------------------------------------------
#   delete the whole object plus all its attrbutes
#
#---------------------------------------------------------------
set dn "cn=Tester,ou=People,o=University of Michigan,c=US"
ldap::delete $handle $dn 

puts "after deleting the whole DN:"
Query $handle

#---------------------------------------------------------------
#   unbind and disconnect from the LDAP server
#
#---------------------------------------------------------------
ldap::unbind     $handle
ldap::disconnect $handle

#-----------------------------------------------------------------------------
#                end of   M A I N  part
#-----------------------------------------------------------------------------