Welcome, guest | Sign In | My Account | Store | Cart

Script to sync Lotus Domino ldap server with IBM Tivoli server using Tcl ldap package.

Platform - Windows 2000 Server.

Tcl, 624 lines
  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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
########################################################  
# Sync Tivoli Directory Server from Lotus Notes Domino.  
# Add new Lotus Notes entries to Tivoli. 
# Delete old Lotus Notes entries from Tivoli and remove entries from roles. 
# Write report and email to respondents. 
########################################################  

puts "\n executing [info script]\n"

# make script drive independent.

set drive [lindex [file split [info script]] 0 ] 

puts "\n proclib = $drive/scripts/TCL/proclib"

########################################################  
# Source utility procs.
########################################################  

source [ file join $drive /scripts/TCL/proclib/checkFile_proc.tcl    ]
source [ file join $drive /scripts/TCL/proclib/smtp_proc.tcl         ]
source [ file join $drive /scripts/TCL/proclib/netSend_proc.tcl      ]
source [ file join $drive /scripts/TCL/proclib/reportHeader_proc.tcl ]

########################################################  
# Source packages. 
########################################################  

package require ldap 
package require Tclx 

###########################################
# Toplevel Proc.
###########################################
proc topProc { reportFileId attr } {

    if { [ catch { extractDominoPersons } r ] == 0 } {

       set dominoPersons $r

       } else { 

       return -code error    $r

    } 

    #############################################
    # Extract internal users from IBM ldap. 
    #############################################

    if { [ catch { extractldapPersons } r ] == 0 } {

       set ldapPersons $r

       } else { 

       return -code error    $r

    } 

    #####################################################################################
    # compare users - extract and delete the entries that exist in ldap but not in Lotus.
    #####################################################################################

    if { [ catch { diffPersons $dominoPersons $ldapPersons $attr } r ] == 0 } {

       puts $r 

       } else { 

       puts $r
       return -code error $r 

    } 
}
###########################################
# Extract dominoPersons from Lotus ldap 
###########################################
proc extractDominoPersons {} {

    global s1 
    global lotusBaseDN

    # Search on objectclass.  Return uid.
    # Return sorted list 
    
    set filter [ list "objectClass=dominoPerson" ]
    
    if { [ catch { ldap::search $s1 $lotusBaseDN $filter {uid} } r ] == 0 } {

	return [ lsort $r ]
	
    } else {

	return -code error $r

    }

}
###########################################
# Extract Internal Users from IBM ldap.
###########################################
proc extractldapPersons {} {

    global s2 
    global ldapBaseDN 

    # Search on objectclass.  Return cn.
    # Return sorted list 

    set filter [ list "objectClass=ldapPerson" ]
    
    if { [ catch { ldap::search $s2 $ldapBaseDN $filter {cn} } r ] == 0 } {

	return [ lsort $r ]
	
    } else {

	return -code error $r

    }

}
###########################################
# Diff Entries.
###########################################
proc diffPersons { dominoPersons ldapPersons attr } {

    global reportFileId
    global ldapServer

    set x 0

    # Extract Domino Uids. 

    foreach i $dominoPersons {
	lappend dominoPersonsList [ lindex $dominoPersons $x 1 1 0 ]
	incr x
    }

    set x 0

    # Extract Tivoli Cns. 
   
    foreach i $ldapPersons {
        lappend ldapPersonsList [ lindex $ldapPersons $x 1 1 0 ]
        incr x
    }

    # Threeway match. 

    foreach { x y z } [ lindex [ intersect3 $dominoPersonsList $ldapPersonsList ] ] {

	set addPersons      $x 
	set matchingPersons $y
	set deletePersons   $z 

    }

    # If the lists are null skip add/delete steps.
    
    set p1 "*****************************************"
    set p2 "* No persons to add"
    set p3 "* No persons to delete"

    # add Domino entry to Tivoli
    
    if { $addPersons == {} || [ string is space $addPersons ] } {

	set t1 [ format "%-20s %s"  " " $p1 ]
	set t2 [ format "%-20s %-30s"  " " $p2 ] 

	puts                \n$t1
	puts                $t2
	puts                $t1\n

	puts  $reportFileId \n$t1
	puts  $reportFileId $t2
	puts  $reportFileId $t1\n
    
    } else {

	    if { [ catch { addPersons $addPersons } r ] == 0 } {

	       set continue true 

	       } else { 

	       return -code error    $r

	    } 

    }

    # delete entry from Tivoli

    if { $deletePersons == {} || [ string is space $deletePersons ] } {

	set t1 [ format "%-20s %s"    " " $p1 ]
	set t2 [ format "%-20s %-30s"  " " $p3 ]

	puts                \n$t1
	puts                $t2
	puts                $t1\n

	puts  $reportFileId \n$t1
	puts  $reportFileId $t2
	puts  $reportFileId $t1\n
    
    } else {

	    if { [ catch { deletePersons   $deletePersons } r ] == 0 } {

	       set continue true 

	       } else { 

	       return -code error    $r

	    } 

    }

}
######################################################################
# Add Persons.
# Add entries which exist on Lotus Domino but do not exist on Tivoli.
######################################################################
proc addPersons { addPersons } {

    global s1
    global s2
    global reportFileId
    global ldapBaseDN
    global lotusBaseDN
    global ldapServer
    global bindDN
    global pw

    set addCount [ llength $addPersons ]

    set p1 "*****************************************"
    set p2 "* Adding Lotus Persons to LDAP"
    set p4 "* Add count is $addCount"

    set t1 [ format "%-20s %30s"  " " $p1 ]
    set t2 [ format "%-20s %-30s" " " $p2 ] 
    set t7 [ format "%-20s %-30s" " " $p4 ] 
    
    puts                $t1
    puts                $t2
    puts                $t7
    puts                $t1
    
    puts  $reportFileId $t1
    puts  $reportFileId $t2
    puts  $reportFileId $t7
    puts  $reportFileId $t1
    
    foreach i $addPersons {

	# select the required attributes from lotus. 

	set filter     "(uid=$i)"
	set attributes {cn givenname sn mail}

	if { [ catch { ldap::search $s1 $lotusBaseDN $filter $attributes } results ] == 0 } {

	   set dn cn=$i\,$ldapBaseDN

	   set objectclass1 top
	   set objectclass2 person
	   set objectclass3 organizationalPerson
	   set objectclass4 inetOrgPerson
	   set objectclass5 ldapPerson
		  
	   # Domino entries may have invalid fields. 
	   # Data cleanup is required.

	   set   cn        $i
	   set   uid       $i

	   set sList [ lindex $results 0 1 ] 

	   if { [ lsearch $sList {*givenname*} ] == -1 } {
	        set givenname "$i" 
	   } else {
	       set index     [ lsearch $sList {*givenname*} ]
               set givenname [ lindex  $sList [ incr index ]]
               unset index 
	   }

	   if { [ lsearch $sList {*sn*} ] == -1 } {
	        set sn "$i" 
	   } else {
               set   index   [ lsearch $sList {*sn*} ]
               set   sn      [ lindex  $sList [ incr index ]]
               unset index 
	   }
	   
	   if { [ lsearch $sList {*mail*} ] == -1 } {
	        set mail "$i" 
	   } else {
	       set   index  [ lsearch $sList {*mail*} ]
	       set   mail   [ lindex $sList [ incr index ] ]
	       unset index 
	   }

	   set userPassword "password"                

           # build attribute list 

	   set list1 [ list objectclass ldapPerson      \
			    objectclass inetOrgPerson  \
			    objectclass organizationalPerson \
			    objectclass person         \
			    objectclass top            \
			    cn $cn                     \
			    uid $uid                   \
			    sn $sn                     \
			    givenname $givenname       \
			    mail $mail                 \
			    userPassword $userPassword ]

	   set g [ string trimleft $givenname givenname= ] 
	   set s [ string trimleft $sn sn= ] 
	   set c [ string trimleft $cn cn= ] 

	   set width [ string length "$s $g \($c\)" ]   

	   set t1 [ format "\n%s %-${width}s %s"  "Adding" "$s $g \($c\)" "to $ldapServer" ]
	   set t2 [ format "\n%s %-${width}s %s"  "   Added" "$s $g \($c\) " "to $ldapServer" ]
	   set t3 [ format "%s"  "   $dn" ]
	   set t4 [ format "\n%s %-${width}s %s"  "   Error adding" "$s $g \($c\) " "to $ldapServer" ]
	   set r "" 
	   set t5 [ format "%s"  "   $r" ]

	   puts                $t1
	   puts                $t3 
	   puts  $reportFileId $t1
	       
           # add entry 
	    
	   if { [ catch { ldap::add $s2 $dn $list1 } r ] == 0 } {

	      puts                $t2
	      puts                $t3 
	      puts  $reportFileId $t2
	      puts  $reportFileId $t3 


           } else {

	       puts                $t4
	       puts                $t3 
	       puts                $t5 
	       puts  $reportFileId $t4
	       puts  $reportFileId $t3
	       puts  [ format "%s"  "   $r" ]
	       puts  $reportFileId [ format "%s"  "   $r" ]

           } 

	   unset dn
	   unset objectclass5
	   unset objectclass4
	   unset objectclass3
	   unset objectclass2
	   unset objectclass1

	   unset cn
	   unset uid
	   unset sn
	   unset givenname
	   unset mail
	   unset userPassword

	   unset list1

	}

    }
}
######################################################################
# Delete Persons.
# Delete entries from Tivoli which exist on Domino but do not exist on Tivoli.
######################################################################
proc deletePersons { deletePersons } {

    global s1
    global s2
    global reportFileId
    global ldapBaseDN
    global attr
    global ldapServer

    set deleteCount [ llength $deletePersons ]

    set p1 "*****************************************"
    set p2 "* Deleting Lotus Persons from LDAP"
    set p4 "* Delete count is $deleteCount"

    set t1 [ format "%-20s %30s"  " " $p1 ]
    set t2 [ format "%-20s %-30s" " " $p2 ] 
    set t7 [ format "%-20s %-30s" " " $p4 ] 
    
    puts                \n$t1
    puts                $t2
    puts                $t7
    puts                $t1
    
    puts  $reportFileId \n$t1
    puts  $reportFileId $t2
    puts  $reportFileId $t7
    puts  $reportFileId $t1

    foreach i $deletePersons {

        # Get the full dn of the target person.
        # Search on CN.

        set attrs [ ldap::search $s2 $ldapBaseDN "(cn=$i)" { cn givenname sn } ] 

        set baseDNPerson [ lindex $attrs 0 0 ]
        set g            [ lindex $attrs 0 1 1 ]
        set s            [ lindex $attrs 0 1 3 ]

	set y "$s $g \($i\)"   
	
	set width [ string length $y ]   

	set t1 [ format "\n%s %-${width}s %s"  "Deleting " $y "from $ldapServer" ]
	set t2 [ format "\n%s %-${width}s %s"  "   Deleted " $y "from $ldapServer" ]
	set t3 [ format "%s"  "   $baseDNPerson" ]
	set t4 [ format "\n%s %-${width}s %s"  "   Error deleting " $y "from $ldapServer" ]
	set r "" 
	set t5 [ format "%s"  "   $r" ]

        # get the roles assigned to the target person.

        set memberOf [ lindex [ ldap::search $s2 $ldapBaseDN "(cn=$i)" {ibm-allGroups} ] 0 1 1 ]

	set replace {}
	set delete  [ list $attr $baseDNPerson ]
	set add     {}

        # remove the target person from roles. 

	foreach i $memberOf {

	   set t6 [ format "\n%s %-${width}s %s"  "Removing " $y "from $i" ]

	   puts                $t6 
	   puts  $reportFileId $t6

	   if { [ catch { ldap::modify $s2 $i $replace $delete $add } r ] == 0 } {

	       set t6 [ format "\n%s %-${width}s %s"  "Removed " $y "from $i" ]

	       puts                $t6
	       puts  $reportFileId $t6

	   } else {

	       set t6 [ format "\n%s %-${width}s %s"  "Error removing " $y "from $i" ]

	       puts                $t6 
	       puts  $reportFileId $t6
	       puts  [ format "%s"  "   $r" ]
	       puts  $reportFileId [ format "%s"  "   $r" ]

	   }
      
        }

        # delete the target person 

	puts                $t1
	puts                $t3 
	puts  $reportFileId $t1

	if { [ catch { ldap::delete $s2 $baseDNPerson } r ] == 0 } {

 	    puts                $t2
 	    puts                $t3 
 	    puts  $reportFileId $t2
 	    puts  $reportFileId $t3 
 
	} else {

	       puts                $t4
	       puts                $t3 
	       puts                $t5 
	       puts  $reportFileId $t4
	       puts  $reportFileId $t3
	       puts  [ format "%s"  "   $r" ]
	       puts  $reportFileId [ format "%s"  "   $r" ]

	}

    }

}
###########################################
# Email Report 
###########################################

proc emailReport { reportFile reportFileId } {

   flush $reportFileId  

   set computerName $::env(COMPUTERNAME)
   set subject "$computerName - Lotus - LDAP Replication"   
   sendSimpleMessage xxx@xxx.com $subject $reportFile

}
######################################
# Control Section.
######################################

######################################
# Set Variables.
######################################

set fileDate    [ clock format [ clock seconds ] -format %Y-%m-%d_%H-%M-%S ]

set reportFile  [ file join $drive reports/ldap/lotusSync_$fileDate\.txt ]

set lotusServer xxxxx 
set ldapServer  yyyyy 
set bindDN      "cn=xxxxx" 
set pw          yxyxyx 
set attr        uniqueMember 

puts "reportfile    = $reportFile\n"

######################################
# Report Header. 
######################################

set reportFileId [ open $reportFile w ]
set header       "$::env(COMPUTERNAME) - Lotus LDAP Replication"
set ldapBaseDN   "ou=xxxxxxx,o=yyyyy,dc=com.au,c=au" 
set lotusBaseDN  "o=aaa" 

reportHeader $reportFileId $header $reportFile

puts               [ format "%-15s %s %s" "Lotus Server"  "- " $lotusServer ]    
puts               [ format "%-15s %s %s" "Tivoli Server" "- " $ldapServer ]    
puts $reportFileId [ format "%-15s %s %s" "Lotus Server" "- " $lotusServer ]    
puts $reportFileId [ format "%-15s %s %s" "Tivoli Server" "- " $ldapServer ]    

################################### 
# Connect and bind to ldap servers
################################### 

set s1 [ ldap::connect $lotusServer 389 ]

::ldap::bind $s1 

set s2 [ ldap::connect $ldapServer 389 ]

::ldap::bind $s2 $bindDN $pw

################################### 
# Global variables.
################################### 

global s1
global s2 
global reportFileId
global ldapBaseDN 
global lotusBaseDN 
global ldapServer 
global bindDN

######################################
# Call toplevel proc. 
######################################

if { [ catch { topProc $reportFileId $attr } r ] == 0 } {

      puts $r 

      } else { 

      puts $r
      puts $reportFileId $r

}

#############################################
# Write Report footer. 
#############################################

set s "*****************************************"
set t1 [ format "%-20s %s" " " $s ]
set s "The End"
set t2 [ format "%-35s    %s" " " $s ]

puts                \n$t1
puts                $t2 
puts                $t1

puts  $reportFileId \n$t1
puts  $reportFileId $t2
puts  $reportFileId $t1

##########################################
# Unbind and disconnect from ldap servers
########################################## 

::ldap::unbind $s1 
::ldap::unbind $s2
::ldap::disconnect $s1
::ldap::disconnect $s2

emailReport $reportFile $reportFileId 
    
close       $reportFileId

######################################
# END.
######################################
Created by Patrick Finnegan on Mon, 22 Nov 2004 (MIT)
Tcl recipes (162)
Patrick Finnegan's recipes (56)

Required Modules

  • (none specified)

Other Information and Tasks