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
|
/** BEGIN COPYRIGHT BLOCK
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
* Copyright (C) 2005 Red Hat, Inc.
* All rights reserved.
* END COPYRIGHT BLOCK **/
/* windows_private.c */
#include "repl.h"
#include "repl5.h"
#include "slap.h"
#include "slapi-plugin.h"
#include "windowsrepl.h"
struct windowsprivate {
Slapi_DN *windows_subtree; /* DN of synchronized subtree (on the windows side) */
Slapi_DN *directory_subtree; /* DN of synchronized subtree on directory side */
/* this simplifies the mapping as it's simply
from the former to the latter container, or
vice versa */
int dirsync_flags;
int dirsync_maxattributecount;
char *dirsync_cookie;
int dirsync_cookie_len;
PRBool dirsync_cookie_has_more;
PRBool create_users_from_dirsync;
PRBool create_groups_from_dirsync;
char *windows_domain;
};
void
windows_init_agreement_from_entry(Repl_Agmt *ra, Slapi_Entry *e)
{
char *tmpstr = NULL;
agmt_set_priv(ra,windows_private_new());
/* DN of entry at root of replicated area */
tmpstr = slapi_entry_attr_get_charptr(e, type_nsds7WindowsReplicaArea);
if (NULL != tmpstr)
{
windows_private_set_windows_subtree(ra, slapi_sdn_new_dn_passin(tmpstr) );
}
tmpstr = slapi_entry_attr_get_charptr(e, type_nsds7DirectoryReplicaArea);
if (NULL != tmpstr)
{
windows_private_set_directory_subtree(ra, slapi_sdn_new_dn_passin(tmpstr) );
}
tmpstr = slapi_entry_attr_get_charptr(e, type_nsds7CreateNewUsers);
if (NULL != tmpstr)
{
windows_private_set_create_users(ra, PR_TRUE);
slapi_ch_free((void**)&tmpstr);
}
else
{
windows_private_set_create_users(ra, PR_FALSE);
}
tmpstr = slapi_entry_attr_get_charptr(e, type_nsds7WindowsDomain);
if (NULL != tmpstr)
{
windows_private_set_windows_domain(ra,tmpstr);
}
}
Dirsync_Private* windows_private_new()
{
Dirsync_Private *dp;
LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_new\n", 0, 0, 0 );
dp = (Dirsync_Private *)slapi_ch_calloc(sizeof(Dirsync_Private),1);
dp->dirsync_maxattributecount = -1;
dp->create_users_from_dirsync = PR_TRUE;
LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_new\n", 0, 0, 0 );
return dp;
}
void windows_agreement_delete(Repl_Agmt *ra)
{
Dirsync_Private *dp = (Dirsync_Private *) agmt_get_priv(ra);
LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_delete\n", 0, 0, 0 );
PR_ASSERT(dp != NULL);
/* DBDB: need to free payoad here */
slapi_ch_free((void **)dp);
LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_delete\n", 0, 0, 0 );
}
/* Returns a copy of the Slapi_DN pointer, no need to free it */
const Slapi_DN* windows_private_get_windows_subtree (const Repl_Agmt *ra)
{
Dirsync_Private *dp;
LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_get_windows_subtree\n", 0, 0, 0 );
PR_ASSERT(ra);
dp = (Dirsync_Private *) agmt_get_priv(ra);
PR_ASSERT (dp);
LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_get_windows_subtree\n", 0, 0, 0 );
return dp->windows_subtree;
}
const char *
windows_private_get_windows_domain(const Repl_Agmt *ra)
{
Dirsync_Private *dp;
LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_get_windows_domain\n", 0, 0, 0 );
PR_ASSERT(ra);
dp = (Dirsync_Private *) agmt_get_priv(ra);
PR_ASSERT (dp);
LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_get_windows_domain\n", 0, 0, 0 );
return dp->windows_domain;
}
static void
windows_private_set_windows_domain(const Repl_Agmt *ra, char *domain)
{
Dirsync_Private *dp;
LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_set_windows_domain\n", 0, 0, 0 );
PR_ASSERT(ra);
dp = (Dirsync_Private *) agmt_get_priv(ra);
PR_ASSERT (dp);
dp->windows_domain = domain;
LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_set_windows_domain\n", 0, 0, 0 );
}
/* Returns a copy of the Slapi_DN pointer, no need to free it */
const Slapi_DN* windows_private_get_directory_subtree (const Repl_Agmt *ra)
{
Dirsync_Private *dp;
LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_get_directory_replarea\n", 0, 0, 0 );
PR_ASSERT(ra);
dp = (Dirsync_Private *) agmt_get_priv(ra);
PR_ASSERT (dp);
LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_get_directory_replarea\n", 0, 0, 0 );
return dp->directory_subtree;
}
/* Takes a copy of the sdn passed in */
void windows_private_set_windows_subtree (const Repl_Agmt *ra,const Slapi_DN* sdn )
{
Dirsync_Private *dp;
LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_set_windows_replarea\n", 0, 0, 0 );
PR_ASSERT(ra);
PR_ASSERT(sdn);
dp = (Dirsync_Private *) agmt_get_priv(ra);
PR_ASSERT (dp);
dp->windows_subtree = slapi_sdn_dup(sdn);
LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_set_windows_replarea\n", 0, 0, 0 );
}
/* Takes a copy of the sdn passed in */
void windows_private_set_directory_subtree (const Repl_Agmt *ra,const Slapi_DN* sdn )
{
Dirsync_Private *dp;
LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_set_directory_replarea\n", 0, 0, 0 );
PR_ASSERT(ra);
PR_ASSERT(sdn);
dp = (Dirsync_Private *) agmt_get_priv(ra);
PR_ASSERT (dp);
dp->directory_subtree = slapi_sdn_dup(sdn);
LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_set_directory_replarea\n", 0, 0, 0 );
}
PRBool windows_private_create_users(const Repl_Agmt *ra)
{
Dirsync_Private *dp;
LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_create_users\n", 0, 0, 0 );
PR_ASSERT(ra);
dp = (Dirsync_Private *) agmt_get_priv(ra);
PR_ASSERT (dp);
LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_create_users\n", 0, 0, 0 );
return dp->create_users_from_dirsync;
}
void windows_private_set_create_users(const Repl_Agmt *ra, PRBool value)
{
Dirsync_Private *dp;
LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_set_create_users\n", 0, 0, 0 );
PR_ASSERT(ra);
dp = (Dirsync_Private *) agmt_get_priv(ra);
PR_ASSERT (dp);
dp->create_users_from_dirsync = value;
LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_set_create_users\n", 0, 0, 0 );
}
/*
This function returns the current Dirsync_Private that's inside
Repl_Agmt ra as a ldap control.
*/
LDAPControl* windows_private_dirsync_control(const Repl_Agmt *ra)
{
LDAPControl *control = NULL;
LDAPControl **lc = &control ;
BerElement *ber;
Dirsync_Private *dp;
LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_dirsync_control\n", 0, 0, 0 );
PR_ASSERT(ra);
dp = (Dirsync_Private *) agmt_get_priv(ra);
PR_ASSERT (dp);
ber = ber_alloc();
ber_printf( ber, "{iio}", dp->dirsync_flags, dp->dirsync_maxattributecount, dp->dirsync_cookie, dp->dirsync_cookie_len );
slapi_build_control( REPL_DIRSYNC_CONTROL_OID, ber, PR_TRUE, &control);
ber_free(ber,1);
LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_dirsync_control\n", 0, 0, 0 );
return control;
}
/*
This function scans the array of controls and updates the Repl_Agmt's
Dirsync_Private if the dirsync control is found.
*/
void windows_private_update_dirsync_control(const Repl_Agmt *ra,LDAPControl **controls )
{
Dirsync_Private *dp;
int foundDirsyncControl;
int i;
LDAPControl *dirsync;
BerElement *ber;
int hasMoreData;
int maxAttributeCount;
BerValue *serverCookie;
int return_value = LDAP_SUCCESS;
LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_update_dirsync_control\n", 0, 0, 0 );
PR_ASSERT(ra);
dp = (Dirsync_Private *) agmt_get_priv(ra);
PR_ASSERT (dp);
if (NULL != controls )
{
foundDirsyncControl = 0;
for ( i = 0; (( controls[i] != NULL ) && ( !foundDirsyncControl )); i++ ) {
foundDirsyncControl = !strcmp( controls[i]->ldctl_oid, REPL_DIRSYNC_CONTROL_OID );
}
if ( !foundDirsyncControl )
{
return_value = LDAP_CONTROL_NOT_FOUND;
goto choke;
}
else
{
dirsync = slapi_dup_control( controls[i-1]);
}
ber = ber_init( &dirsync->ldctl_value ) ;
if (ber_scanf( ber, "{iiO}", &hasMoreData, &maxAttributeCount, &serverCookie) == LBER_ERROR)
{
return_value = LDAP_CONTROL_NOT_FOUND;
goto choke;
}
slapi_ch_free(&dp->dirsync_cookie);
dp->dirsync_cookie = ( char* ) slapi_ch_malloc(serverCookie->bv_len + 1);
memcpy(dp->dirsync_cookie, serverCookie->bv_val, serverCookie->bv_len);
dp->dirsync_cookie_len = (int) serverCookie->bv_len; /* XXX shouldn't cast? */
/* dp->dirsync_maxattributecount = maxAttributeCount; We don't need to keep this */
dp->dirsync_cookie_has_more = hasMoreData;
choke:
ber_bvfree(serverCookie);
ber_free(ber,1);
}
else
{
return_value = LDAP_CONTROL_NOT_FOUND;
}
LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_update_dirsync_control\n", 0, 0, 0 );
/* return return_value; */
}
PRBool windows_private_dirsync_has_more(const Repl_Agmt *ra)
{
Dirsync_Private *dp;
LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_dirsync_has_more\n", 0, 0, 0 );
PR_ASSERT(ra);
dp = (Dirsync_Private *) agmt_get_priv(ra);
PR_ASSERT (dp);
LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_dirsync_has_more\n", 0, 0, 0 );
return dp->dirsync_cookie_has_more;
}
void windows_private_null_dirsync_cookie(const Repl_Agmt *ra)
{
Dirsync_Private *dp;
LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_null_dirsync_control\n", 0, 0, 0 );
dp = (Dirsync_Private *) agmt_get_priv(ra);
PR_ASSERT (dp);
dp->dirsync_cookie_len = 0;
slapi_ch_free(&dp->dirsync_cookie);
dp->dirsync_cookie = NULL;
LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_null_dirsync_control\n", 0, 0, 0 );
}
static
Slapi_Mods *windows_private_get_cookie_mod(Dirsync_Private *dp, int modtype)
{
Slapi_Mods *smods = NULL;
smods = slapi_mods_new();
slapi_mods_add( smods, modtype,
"nsds7DirsyncCookie", dp->dirsync_cookie_len , dp->dirsync_cookie);
return smods;
}
/* writes the current cookie into dse.ldif under the replication agreement entry
returns: ldap result code of the operation. */
int
windows_private_save_dirsync_cookie(const Repl_Agmt *ra)
{
Dirsync_Private *dp = NULL;
Slapi_PBlock *pb = NULL;
const char* dn = NULL;
Slapi_DN* sdn = NULL;
int rc = 0;
Slapi_Mods *mods = NULL;
LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_save_dirsync_cookie\n", 0, 0, 0 );
PR_ASSERT(ra);
dp = (Dirsync_Private *) agmt_get_priv(ra);
PR_ASSERT (dp);
pb = slapi_pblock_new ();
mods = windows_private_get_cookie_mod(dp, LDAP_MOD_REPLACE);
sdn = slapi_sdn_dup( agmt_get_dn_byref(ra) );
dn = slapi_sdn_get_dn(sdn);
slapi_modify_internal_set_pb (pb, dn, slapi_mods_get_ldapmods_byref(mods), NULL, NULL,
repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0);
slapi_modify_internal_pb (pb);
slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
if (rc == LDAP_NO_SUCH_ATTRIBUTE)
{ /* try again, but as an add instead */
mods = windows_private_get_cookie_mod(dp, LDAP_MOD_ADD);
slapi_modify_internal_set_pb (pb, dn, slapi_mods_get_ldapmods_byref(mods), NULL, NULL,
repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0);
slapi_modify_internal_pb (pb);
slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
}
slapi_pblock_destroy (pb);
slapi_mods_free(&mods);
slapi_sdn_free(&sdn);
LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_save_dirsync_cookie\n", 0, 0, 0 );
return rc;
}
/* reads the cookie in dse.ldif to the replication agreement entry
returns: ldap result code of ldap operation, or
LDAP_NO_SUCH_ATTRIBUTE. (this is the equilivent of a null cookie) */
int windows_private_load_dirsync_cookie(const Repl_Agmt *ra)
{
Dirsync_Private *dp = NULL;
Slapi_PBlock *pb = NULL;
Slapi_DN* sdn = NULL;
int rc = 0;
Slapi_Entry *entry = NULL;
char* cookie = NULL;
Slapi_Attr *attr = NULL;
LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_load_dirsync_cookie\n", 0, 0, 0 );
PR_ASSERT(ra);
dp = (Dirsync_Private *) agmt_get_priv(ra);
PR_ASSERT (dp);
pb = slapi_pblock_new ();
sdn = slapi_sdn_dup( agmt_get_dn_byref(ra) );
rc = slapi_search_internal_get_entry(sdn, NULL, &entry,
repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION));
if (rc == 0)
{
rc= slapi_entry_attr_find( entry, type_nsds7DirsyncCookie, &attr );
if (attr)
{
struct berval **vals;
rc = slapi_attr_get_bervals_copy(attr, &vals );
if (vals)
{
dp->dirsync_cookie_len = (int) (vals[0])->bv_len;
slapi_ch_free(&dp->dirsync_cookie);
dp->dirsync_cookie = ( char* ) slapi_ch_malloc(dp->dirsync_cookie_len + 1);
memcpy(dp->dirsync_cookie,(vals[0]->bv_val), (vals[0])->bv_len+1);
}
ber_bvecfree(vals);
/* we do not free attr */
}
else
{
rc = LDAP_NO_SUCH_ATTRIBUTE;
}
}
if (entry)
{
slapi_entry_free(entry);
}
slapi_sdn_free( &sdn);
slapi_pblock_destroy (pb);
LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_load_dirsync_cookie\n", 0, 0, 0 );
return rc;
}
|