-
Notifications
You must be signed in to change notification settings - Fork 81
/
mep.c
2856 lines (2464 loc) · 98.3 KB
/
mep.c
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** BEGIN COPYRIGHT BLOCK
* This Program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; version 2 of the License.
*
* This Program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place, Suite 330, Boston, MA 02111-1307 USA.
*
* In addition, as a special exception, Red Hat, Inc. gives You the additional
* right to link the code of this Program with code not covered under the GNU
* General Public License ("Non-GPL Code") and to distribute linked combinations
* including the two, subject to the limitations in this paragraph. Non-GPL Code
* permitted under this exception must only link to the code of this Program
* through those well defined interfaces identified in the file named EXCEPTION
* found in the source code files (the "Approved Interfaces"). The files of
* Non-GPL Code may instantiate templates or use macros or inline functions from
* the Approved Interfaces without causing the resulting work to be covered by
* the GNU General Public License. Only Red Hat, Inc. may make changes or
* additions to the list of Approved Interfaces. You must obey the GNU General
* Public License in all respects for all of the Program code and other code used
* in conjunction with the Program except the Non-GPL Code covered by this
* exception. If you modify this file, you may extend this exception to your
* version of the file, but you are not obligated to do so. If you do not wish to
* provide this exception without modification, you must delete this exception
* statement from your version and license this file solely under the GPL without
* exception.
*
*
* Copyright (C) 2010 Red Hat, Inc.
* All rights reserved.
* END COPYRIGHT BLOCK **/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
/*
* Managed Entries Plug-in
*/
#include "mep.h"
#include "slapi-private.h"
/*
* Plug-in globals
*/
static PRCList *g_mep_config = NULL;
static Slapi_RWLock *g_mep_config_lock;
static void *_PluginID = NULL;
static Slapi_DN *_PluginDN = NULL;
static Slapi_DN *_ConfigAreaDN = NULL;
static int g_plugin_started = 0;
static Slapi_PluginDesc pdesc = { MEP_FEATURE_DESC,
VENDOR,
DS_PACKAGE_VERSION,
MEP_PLUGIN_DESC };
/*
* Plug-in management functions
*/
int mep_init(Slapi_PBlock * pb);
static int mep_start(Slapi_PBlock * pb);
static int mep_close(Slapi_PBlock * pb);
static int mep_postop_init(Slapi_PBlock * pb);
static int mep_internal_postop_init(Slapi_PBlock *pb);
/*
* Operation callbacks (where the real work is done)
*/
static int mep_mod_post_op(Slapi_PBlock *pb);
static int mep_add_post_op(Slapi_PBlock *pb);
static int mep_del_post_op(Slapi_PBlock *pb);
static int mep_modrdn_post_op(Slapi_PBlock *pb);
static int mep_pre_op(Slapi_PBlock *pb, int modop);
static int mep_mod_pre_op(Slapi_PBlock *pb);
static int mep_add_pre_op(Slapi_PBlock *pb);
static int mep_del_pre_op(Slapi_PBlock *pb);
static int mep_modrdn_pre_op(Slapi_PBlock *pb);
/*
* Config cache management functions
*/
static int mep_load_config();
static void mep_delete_config();
static int mep_parse_config_entry(Slapi_Entry * e, int apply);
static void mep_free_config_entry(struct configEntry ** entry);
/*
* helpers
*/
static Slapi_DN *mep_get_sdn(Slapi_PBlock * pb);
static Slapi_DN *mep_get_config_area();
static void mep_set_config_area(Slapi_DN *sdn);
static int mep_dn_is_config(Slapi_DN *sdn);
static int mep_dn_is_template(Slapi_DN *dn);
static void mep_find_config(Slapi_Entry *e, struct configEntry **config);
static void mep_find_config_by_template_dn(Slapi_DN *template_dn,
struct configEntry **config);
static int mep_oktodo(Slapi_PBlock *pb);
static int mep_isrepl(Slapi_PBlock *pb);
static Slapi_Entry *mep_create_managed_entry(struct configEntry *config,
Slapi_Entry *origin);
static void mep_add_managed_entry(struct configEntry *config,
Slapi_Entry *origin);
static void mep_rename_managed_entry(Slapi_Entry *origin,
Slapi_DN *new_dn, Slapi_DN *old_dn);
static Slapi_Mods *mep_get_mapped_mods(struct configEntry *config,
Slapi_Entry *origin, char **mapped_dn);
static int mep_parse_mapped_attr(char *mapping, Slapi_Entry *origin,
char **type, char **value);
static int mep_is_managed_entry(Slapi_Entry *e);
static int mep_is_mapped_attr(Slapi_Entry *template, char *type);
static int mep_has_tombstone_value(Slapi_Entry * e);
static int mep_parse_mapped_origin_attr(char *mapping, char **origin_type);
static int mep_is_mapped_origin_attr(char **vals, char *type);
static char** mep_extract_origin_attrs(Slapi_Entry *entry);
/*
* Config cache locking functions
*/
void
mep_config_read_lock()
{
slapi_rwlock_rdlock(g_mep_config_lock);
}
void
mep_config_write_lock()
{
slapi_rwlock_wrlock(g_mep_config_lock);
}
void
mep_config_unlock()
{
slapi_rwlock_unlock(g_mep_config_lock);
}
/*
* Plugin identity functions
*/
void
mep_set_plugin_id(void *pluginID)
{
_PluginID = pluginID;
}
void *
mep_get_plugin_id()
{
return _PluginID;
}
void
mep_set_plugin_sdn(Slapi_DN *pluginDN)
{
_PluginDN = pluginDN;
}
Slapi_DN *
mep_get_plugin_sdn()
{
return _PluginDN;
}
static int plugin_is_betxn = 0;
/*
* Plug-in initialization functions
*/
int
mep_init(Slapi_PBlock *pb)
{
int status = 0;
char *plugin_identity = NULL;
Slapi_Entry *plugin_entry = NULL;
char *plugin_type = NULL;
int preadd = SLAPI_PLUGIN_PRE_ADD_FN;
int premod = SLAPI_PLUGIN_PRE_MODIFY_FN;
int predel = SLAPI_PLUGIN_PRE_DELETE_FN;
int premdn = SLAPI_PLUGIN_PRE_MODRDN_FN;
slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
"--> mep_init\n");
if ((slapi_pblock_get(pb, SLAPI_PLUGIN_CONFIG_ENTRY, &plugin_entry) == 0) &&
plugin_entry &&
(plugin_type = slapi_entry_attr_get_charptr(plugin_entry, "nsslapd-plugintype")) &&
plugin_type && strstr(plugin_type, "betxn")) {
plugin_is_betxn = 1;
preadd = SLAPI_PLUGIN_BE_TXN_PRE_ADD_FN;
premod = SLAPI_PLUGIN_BE_TXN_PRE_MODIFY_FN;
predel = SLAPI_PLUGIN_BE_TXN_PRE_DELETE_FN;
premdn = SLAPI_PLUGIN_BE_TXN_PRE_MODRDN_FN;
}
slapi_ch_free_string(&plugin_type);
/* Store the plugin identity for later use.
* Used for internal operations. */
slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &plugin_identity);
PR_ASSERT(plugin_identity);
mep_set_plugin_id(plugin_identity);
/* Register callbacks */
if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
SLAPI_PLUGIN_VERSION_01) != 0 ||
slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN,
(void *) mep_start) != 0 ||
slapi_pblock_set(pb, SLAPI_PLUGIN_CLOSE_FN,
(void *) mep_close) != 0 ||
slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
(void *) &pdesc) != 0 ||
slapi_pblock_set(pb, premod, (void *) mep_mod_pre_op) != 0 ||
slapi_pblock_set(pb, preadd, (void *) mep_add_pre_op) != 0 ||
slapi_pblock_set(pb, predel, (void *) mep_del_pre_op) != 0 ||
slapi_pblock_set(pb, premdn, (void *) mep_modrdn_pre_op) != 0) {
slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
"mep_init: failed to register plugin\n");
status = -1;
}
if (!status && !plugin_is_betxn &&
slapi_register_plugin("internalpostoperation", /* op type */
1, /* Enabled */
"mep_init", /* this function desc */
mep_internal_postop_init, /* init func */
MEP_INT_POSTOP_DESC, /* plugin desc */
NULL, /* ? */
plugin_identity /* access control */
)) {
slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
"mep_init: failed to register internalpostoperation plugin\n");
status = -1;
}
if (!status) {
plugin_type = plugin_is_betxn ? "betxnpostoperation" : "postoperation";
if (slapi_register_plugin(plugin_type, /* op type */
1, /* Enabled */
"mep_init", /* this function desc */
mep_postop_init, /* init func for post op */
MEP_POSTOP_DESC, /* plugin desc */
NULL, /* ? */
plugin_identity /* access control */
)) {
slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
"mep_init: failed to register plugin\n");
status = -1;
}
}
slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
"<-- mep_init\n");
return status;
}
/* not used when using plugin as a betxn plugin - betxn plugins are called for both internal and external ops */
static int
mep_internal_postop_init(Slapi_PBlock *pb)
{
int status = 0;
if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
SLAPI_PLUGIN_VERSION_01) != 0 ||
slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
(void *) &pdesc) != 0 ||
slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_ADD_FN,
(void *) mep_add_post_op) != 0 ||
slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_DELETE_FN,
(void *) mep_del_post_op) != 0 ||
slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_MODIFY_FN,
(void *) mep_mod_post_op) != 0 ||
slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_MODRDN_FN,
(void *) mep_modrdn_post_op) != 0) {
slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
"mep_internal_postop_init: failed to register plugin\n");
status = -1;
}
return status;
}
static int
mep_postop_init(Slapi_PBlock *pb)
{
int status = 0;
int addfn = SLAPI_PLUGIN_POST_ADD_FN;
int delfn = SLAPI_PLUGIN_POST_DELETE_FN;
int modfn = SLAPI_PLUGIN_POST_MODIFY_FN;
int mdnfn = SLAPI_PLUGIN_POST_MODRDN_FN;
if (plugin_is_betxn) {
addfn = SLAPI_PLUGIN_BE_TXN_POST_ADD_FN;
delfn = SLAPI_PLUGIN_BE_TXN_POST_DELETE_FN;
modfn = SLAPI_PLUGIN_BE_TXN_POST_MODIFY_FN;
mdnfn = SLAPI_PLUGIN_BE_TXN_POST_MODRDN_FN;
}
if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
SLAPI_PLUGIN_VERSION_01) != 0 ||
slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
(void *) &pdesc) != 0 ||
slapi_pblock_set(pb, addfn, (void *) mep_add_post_op) != 0 ||
slapi_pblock_set(pb, delfn, (void *) mep_del_post_op) != 0 ||
slapi_pblock_set(pb, modfn, (void *) mep_mod_post_op) != 0 ||
slapi_pblock_set(pb, mdnfn, (void *) mep_modrdn_post_op) != 0) {
slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
"mep_postop_init: failed to register plugin\n");
status = -1;
}
return status;
}
/*
* mep_start()
*
* Creates config lock and loads config cache.
*/
static int
mep_start(Slapi_PBlock * pb)
{
Slapi_DN *plugindn = NULL;
char *config_area = NULL;
slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
"--> mep_start\n");
/* Check if we're already started */
if (g_plugin_started) {
goto done;
}
g_mep_config_lock = slapi_new_rwlock();
if (!g_mep_config_lock) {
slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
"mep_start: lock creation failed\n");
return -1;
}
/*
* Get the plug-in target dn from the system
* and store it for future use. */
slapi_pblock_get(pb, SLAPI_TARGET_SDN, &plugindn);
if (NULL == plugindn || 0 == slapi_sdn_get_ndn_len(plugindn)) {
slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
"mep_start: unable to retrieve plugin dn\n");
return -1;
}
mep_set_plugin_sdn(slapi_sdn_dup(plugindn));
/* Set the alternate config area if one is defined. */
slapi_pblock_get(pb, SLAPI_PLUGIN_CONFIG_AREA, &config_area);
if (config_area) {
mep_set_config_area(slapi_sdn_new_normdn_byval(config_area));
}
/*
* Load the config cache
*/
g_mep_config = (PRCList *)slapi_ch_calloc(1, sizeof(struct configEntry));
PR_INIT_CLIST(g_mep_config);
if (mep_load_config() != 0) {
slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
"mep_start: unable to load plug-in configuration\n");
return -1;
}
g_plugin_started = 1;
slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
"managed entries plug-in: ready for service\n");
slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
"<-- mep_start\n");
done:
return 0;
}
/*
* mep_close()
*
* Cleans up the config cache.
*/
static int
mep_close(Slapi_PBlock * pb)
{
slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
"--> mep_close\n");
if (!g_plugin_started) {
goto done;
}
mep_config_write_lock();
g_plugin_started = 0;
mep_delete_config();
mep_config_unlock();
slapi_ch_free((void **)&g_mep_config);
slapi_sdn_free(&_PluginDN);
slapi_sdn_free(&_ConfigAreaDN);
/* We explicitly don't destroy the config lock here. If we did,
* there is the slight possibility that another thread that just
* passed the g_plugin_started check is about to try to obtain
* a reader lock. We leave the lock around so these threads
* don't crash the process. If we always check the started
* flag again after obtaining a reader lock, no free'd resources
* will be used. */
done:
slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
"<-- mep_close\n");
return 0;
}
PRCList *
mep_get_config()
{
return g_mep_config;
}
/*
* config looks like this
* - cn=Managed Entries,cn=plugins,cn=config
* --- cn=user private groups,...
* --- cn=etc,...
*/
static int
mep_load_config()
{
int status = 0;
int result;
int i;
Slapi_PBlock *search_pb;
Slapi_Entry **entries = NULL;
slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
"--> mep_load_config\n");
/* Clear out any old config. */
mep_config_write_lock();
mep_delete_config();
search_pb = slapi_pblock_new();
/* If an alternate config area is configured, find
* the config entries that are beneath it, otherwise
* we load the entries beneath our top-level plug-in
* config entry. */
if (mep_get_config_area()) {
/* Find the config entries beneath the alternate config area. */
slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
"mep_load_config: Looking for config entries "
"beneath \"%s\".\n", slapi_sdn_get_ndn(mep_get_config_area()));
slapi_search_internal_set_pb(search_pb, slapi_sdn_get_ndn(mep_get_config_area()),
LDAP_SCOPE_SUBTREE, "objectclass=*",
NULL, 0, NULL, NULL, mep_get_plugin_id(), 0);
} else {
/* Find the config entries beneath our plugin entry. */
slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
"mep_load_config: Looking for config entries "
"beneath \"%s\".\n",
slapi_sdn_get_ndn(mep_get_plugin_sdn()));
slapi_search_internal_set_pb(search_pb,
slapi_sdn_get_ndn(mep_get_plugin_sdn()),
LDAP_SCOPE_SUBTREE, "objectclass=*",
NULL, 0, NULL, NULL, mep_get_plugin_id(), 0);
}
slapi_search_internal_pb(search_pb);
slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
if (LDAP_SUCCESS != result) {
if (mep_get_config_area() && (result == LDAP_NO_SUCH_OBJECT)) {
slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
"mep_load_config: Config container \"%s\" does "
"not exist.\n", slapi_sdn_get_ndn(mep_get_config_area()));
goto cleanup;
} else {
status = -1;
goto cleanup;
}
}
slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
&entries);
/* Loop through all of the entries we found and parse them. */
for (i = 0; entries && (entries[i] != NULL); i++) {
/* We don't care about the status here because we may have
* some invalid config entries, but we just want to continue
* looking for valid ones. */
mep_parse_config_entry(entries[i], 1);
}
cleanup:
slapi_free_search_results_internal(search_pb);
slapi_pblock_destroy(search_pb);
mep_config_unlock();
slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
"<-- mep_load_config\n");
return status;
}
/*
* mep_parse_config_entry()
*
* Parses a single config entry. If apply is non-zero, then
* we will load and start using the new config. You can simply
* validate config without making any changes by setting apply
* to 0.
*
* Returns 0 if the entry is valid and -1 if it is invalid.
*/
static int
mep_parse_config_entry(Slapi_Entry * e, int apply)
{
char *value;
struct configEntry *entry = NULL;
struct configEntry *config_entry;
PRCList *list;
int entry_added = 0;
int ret = 0;
slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
"--> mep_parse_config_entry\n");
/* If this is the main plug-in
* config entry, just bail. */
if ((slapi_sdn_compare(mep_get_plugin_sdn(), slapi_entry_get_sdn(e)) == 0) ||
(mep_get_config_area() && (slapi_sdn_compare(mep_get_config_area(),
slapi_entry_get_sdn(e)) == 0))) {
ret = -1;
goto bail;
}
entry = (struct configEntry *)slapi_ch_calloc(1, sizeof(struct configEntry));
if (NULL == entry) {
ret = -1;
goto bail;
}
entry->sdn = slapi_sdn_dup(slapi_entry_get_sdn(e));
if(entry->sdn == NULL){
slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
"mep_parse_config_entry: Error "
"reading dn from config entry\n");
ret = -1;
goto bail;
}
slapi_log_error(SLAPI_LOG_CONFIG, MEP_PLUGIN_SUBSYSTEM,
"----------> dn [%s]\n", slapi_sdn_get_dn(entry->sdn));
/* Load the origin scope */
value = slapi_entry_attr_get_charptr(e, MEP_SCOPE_TYPE);
if (value) {
entry->origin_scope = value;
} else {
slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
"mep_parse_config_entry: The %s config "
"setting is required for config entry \"%s\".\n",
MEP_SCOPE_TYPE, slapi_sdn_get_dn(entry->sdn));
ret = -1;
goto bail;
}
/* Load the origin filter */
value = slapi_entry_attr_get_charptr(e, MEP_FILTER_TYPE);
if (value) {
/* Convert to a Slapi_Filter to improve performance. */
if (NULL == (entry->origin_filter = slapi_str2filter(value))) {
slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM ,
"mep_parse_config_entry: Invalid search filter in "
"%s config setting for config entry \"%s\" "
"(filter = \"%s\").\n", MEP_FILTER_TYPE, slapi_sdn_get_dn(entry->sdn), value);
ret = -1;
}
slapi_ch_free_string(&value);
if (ret != 0) {
goto bail;
}
} else {
slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
"mep_parse_config_entry: The %s config "
"setting is required for config entry \"%s\".\n",
MEP_FILTER_TYPE, slapi_sdn_get_dn(entry->sdn));
ret = -1;
goto bail;
}
/* Load the managed base */
value = slapi_entry_attr_get_charptr(e, MEP_MANAGED_BASE_TYPE);
if (value) {
entry->managed_base = value;
} else {
slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
"mep_parse_config_entry: The %s config "
"setting is required for config entry \"%s\".\n",
MEP_MANAGED_BASE_TYPE, slapi_sdn_get_dn(entry->sdn));
ret = -1;
goto bail;
}
/* Load the managed template */
value = slapi_entry_attr_get_charptr(e, MEP_MANAGED_TEMPLATE_TYPE);
if (value) {
Slapi_Entry *test_entry = NULL;
entry->template_sdn = slapi_sdn_new_dn_passin(value);
value = NULL; /* entry->template_sdn owns value now */
/* Fetch the managed entry template */
slapi_search_internal_get_entry(entry->template_sdn, 0,
&entry->template_entry, mep_get_plugin_id());
if (entry->template_entry == NULL) {
slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
"mep_parse_config_entry: The managed entry "
"template \"%s\" does not exist. Please "
"add it or correct the %s config setting for "
"config entry \"%s\"\n", slapi_sdn_get_dn(entry->template_sdn),
MEP_MANAGED_TEMPLATE_TYPE, slapi_sdn_get_dn(entry->sdn));
ret = -1;
goto bail;
}
/* Validate the template entry by creating a test managed
* entry and running a schema check on it */
test_entry = mep_create_managed_entry(entry, NULL);
if (test_entry == NULL) {
slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
"mep_parse_config_entry: Unable to create "
"a test managed entry from managed entry "
"template \"%s\". Please check the template "
"entry for errors.\n", slapi_sdn_get_dn(entry->template_sdn));
ret = -1;
goto bail;
}
/* Check the schema */
if (slapi_entry_schema_check(NULL, test_entry) != 0) {
slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
"mep_parse_config_entry: Test managed "
"entry created from managed entry template "
"\"%s\" violates the schema. Please check "
"the template entry for schema errors.\n",
slapi_sdn_get_dn(entry->template_sdn));
slapi_entry_free(test_entry);
ret = -1;
goto bail;
}
/*
* Extract the origin attrs from the template entry
*/
entry->origin_attrs = mep_extract_origin_attrs(entry->template_entry);
/* Dispose of the test entry */
slapi_entry_free(test_entry);
} else {
slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
"mep_parse_config_entry: The %s config "
"setting is required for config entry \"%s\".\n",
MEP_MANAGED_TEMPLATE_TYPE, slapi_sdn_get_dn(entry->sdn));
ret = -1;
goto bail;
}
/* If we were only called to validate config, we can
* just bail out before applying the config changes */
if (apply == 0) {
goto bail;
}
/* Add the config object to the list. We order by scope. */
if (!PR_CLIST_IS_EMPTY(g_mep_config)) {
list = PR_LIST_HEAD(g_mep_config);
while (list != g_mep_config) {
config_entry = (struct configEntry *) list;
/* If the config entry we are adding has a scope that is
* a child of the scope of the current list item, we insert
* the entry before that list item. */
if (slapi_dn_issuffix(entry->origin_scope, config_entry->origin_scope)) {
PR_INSERT_BEFORE(&(entry->list), list);
slapi_log_error(SLAPI_LOG_CONFIG, MEP_PLUGIN_SUBSYSTEM,
"store [%s] before [%s] \n", slapi_sdn_get_dn(entry->sdn),
slapi_sdn_get_dn(config_entry->sdn));
entry_added = 1;
break;
}
list = PR_NEXT_LINK(list);
/* If we hit the end of the list, add to the tail. */
if (g_mep_config == list) {
PR_INSERT_BEFORE(&(entry->list), list);
slapi_log_error(SLAPI_LOG_CONFIG, MEP_PLUGIN_SUBSYSTEM,
"store [%s] at tail\n", slapi_sdn_get_dn(entry->sdn));
entry_added = 1;
break;
}
}
} else {
/* first entry */
PR_INSERT_LINK(&(entry->list), g_mep_config);
slapi_log_error(SLAPI_LOG_CONFIG, MEP_PLUGIN_SUBSYSTEM,
"store [%s] at head \n", slapi_sdn_get_dn(entry->sdn));
entry_added = 1;
}
bail:
if (0 == entry_added) {
/* Don't log error if we weren't asked to apply config */
if ((apply != 0) && (entry != NULL)) {
slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
"mep_parse_config_entry: Invalid config entry "
"[%s] skipped\n", slapi_sdn_get_dn(entry->sdn));
}
mep_free_config_entry(&entry);
} else {
ret = 0;
}
slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
"<-- mep_parse_config_entry\n");
return ret;
}
static void
mep_free_config_entry(struct configEntry ** entry)
{
struct configEntry *e = *entry;
if (e == NULL)
return;
if (e->sdn) {
slapi_log_error(SLAPI_LOG_CONFIG, MEP_PLUGIN_SUBSYSTEM,
"freeing config entry [%s]\n", slapi_sdn_get_dn(e->sdn));
slapi_sdn_free(&e->sdn);
}
if (e->origin_scope) {
slapi_ch_free_string(&e->origin_scope);
}
if (e->origin_filter) {
slapi_filter_free(e->origin_filter, 1);
}
if (e->managed_base) {
slapi_ch_free_string(&e->managed_base);
}
if (e->template_sdn) {
slapi_sdn_free(&e->template_sdn);
}
if (e->template_entry) {
slapi_entry_free(e->template_entry);
}
if(e->origin_attrs){
slapi_ch_array_free(e->origin_attrs);
}
slapi_ch_free((void **) entry);
}
static void
mep_delete_configEntry(PRCList *entry)
{
PR_REMOVE_LINK(entry);
mep_free_config_entry((struct configEntry **) &entry);
}
static void
mep_delete_config()
{
PRCList *list;
/* Delete the config cache. */
while (!PR_CLIST_IS_EMPTY(g_mep_config)) {
list = PR_LIST_HEAD(g_mep_config);
mep_delete_configEntry(list);
}
return;
}
/*
* Helper functions
*/
/*
* mep_parse_mapped_origin_attr()
*
* Parses a mapped attribute setting from a template and
* grabs the attribute name and places it in origin_type.
*
* This is used to determine if a modify operation needs
* to update the managed entry.
*/
static int
mep_parse_mapped_origin_attr(char *mapping, char **origin_type)
{
int ret = 0;
char *p = NULL;
char *end = NULL;
char *var_start = NULL;
/* reset the pointer for origin_type as this func is usually in a loop */
*origin_type = NULL;
/* split out the type from the value (use the first ':') */
if ((p = strchr(mapping, ':')) == NULL) {
slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
"mep_parse_mapped_origin_attr: Value for mapped attribute "
"is not in the correct format. (value: \"%s\").\n",
mapping);
ret = 1;
goto bail;
}
/* Ensure the type is not empty. */
if (p == mapping) {
slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
"mep_parse_mapped_origin_attr: Value for mapped attribute "
"is not in the correct format. The type is missing. "
"(value: \"%s\").\n",
mapping);
ret = 1;
goto bail;
}
/* Terminate the type so we can use it as a string. */
*p = '\0';
/* Advance p to point to the beginning of the value. */
p++;
while (*p == ' ') {
p++;
}
/* Make end point to the last character that we want in the value. */
end = p + strlen(p) - 1;
/* Find the variable that we need to substitute. */
for (; p <= end; p++) {
if (*p == '$') {
if (p == end) {
slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
"mep_parse_mapped_origin_attr: Invalid mapped "
"attribute value for type \"%s\".\n", mapping);
ret = 1;
goto bail;
}
if (*(p + 1) == '$') {
/* This is an escaped $. Eliminate the escape character
* to prevent if from being a part of the value. */
p++;
memmove(p, p+1, end-(p+1)+1);
*end = '\0';
end--;
} else {
int quoted = 0;
/* We found a variable. Terminate the pre
* string and process the variable. */
*p = '\0';
p++;
/* Check if the variable name is quoted. If it is, we skip past
* the quoting brace to avoid putting it in the mapped value. */
if (*p == '{') {
quoted = 1;
if (p < end) {
p++;
} else {
slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
"mep_parse_mapped_origin_attr: Invalid mapped "
"attribute value for type \"%s\".\n", mapping);
ret = 1;
goto bail;
}
}
/* We should be pointing at the variable name now. */
var_start = p;
/* Move the pointer to the end of the variable name. We
* stop at the first character that is not legal for use
* in an attribute description. */
while ((p < end) && IS_ATTRDESC_CHAR(*p)) {
p++;
}
/* If the variable is quoted and this is not a closing
* brace, there is a syntax error in the mapping rule. */
if (quoted && (*p != '}')) {
slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
"mep_parse_mapped_origin_attr: Invalid mapped "
"attribute value for type \"%s\".\n", mapping);
ret = 1;
goto bail;
}
/* Check for a missing variable name. */
if (p == var_start) {
break;
}
if (p == end) {
/* Set the map type. In this case, p could be
* pointing at either the last character of the
* map type, or at the first character after the
* map type. If the character is valid for use
* in an attribute description, we consider it
* to be a part of the map type. */
if (IS_ATTRDESC_CHAR(*p)) {
*origin_type = strndup(var_start, p - var_start + 1);
/* There is no post string. */
} else {
*origin_type = strndup(var_start, p - var_start);
}
} else {
/* Set the map type. In this case, p is pointing
* at the first character after the map type. */
*origin_type = strndup(var_start, p - var_start);
}
/* We only support a single variable, so we're done. */
break;
}
}
}
bail:
if (ret != 0) {
slapi_ch_free_string(origin_type);
}
return ret;
}
/*
* mep_extract_origin_attrs
*
* Extract the attributes from the template that reside on
* the origin entry that trigger updates to the managed entry.
*/
static char **
mep_extract_origin_attrs(Slapi_Entry *template)
{
char **vals = NULL;
char *origin_type = NULL;
char **origin_attrs = NULL;
int i;
if (template) {
vals = slapi_entry_attr_get_charray(template, MEP_MAPPED_ATTR_TYPE);
for (i = 0; vals && vals[i]; ++i) {
if (mep_parse_mapped_origin_attr(vals[i], &origin_type) == 0) {
slapi_ch_array_add(&origin_attrs,origin_type);
}
}
slapi_ch_array_free(vals);
}
return origin_attrs;
}