| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797 |
- /** BEGIN COPYRIGHT BLOCK
- * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
- * Copyright (C) 2005 Red Hat, Inc.
- * All rights reserved.
- *
- * License: GPL (version 3 or any later version).
- * See LICENSE for details.
- * END COPYRIGHT BLOCK **/
- #ifdef HAVE_CONFIG_H
- # include <config.h>
- #endif
- #include <stdio.h>
- #include <string.h>
- #include "portable.h"
- #include "slapi-plugin.h"
- #include "slap.h"
- #include "prthread.h"
- #include "prlock.h"
- #include "prerror.h"
- #include "prcvar.h"
- #include "prio.h"
- #include <sys/stat.h>
- #define REFERINT_DEFAULT_FILE_MODE S_IRUSR | S_IWUSR
- #define REFERINT_PLUGIN_SUBSYSTEM "referint-plugin" /* used for logging */
- #define REFERINT_PREOP_DESC "referint preop plugin"
- #define REFERINT_ATTR_DELAY "referint-update-delay"
- #define REFERINT_ATTR_LOGCHANGES "referint-logchanges"
- #define REFERINT_ATTR_LOGFILE "referint-logfile"
- #define REFERINT_ATTR_MEMBERSHIP "referint-membership-attr"
- #define MAX_LINE 2048
- #define READ_BUFSIZE 4096
- #define MY_EOF 0
- #define STARTUP 2
- typedef struct referint_config {
- int delay;
- char *logfile;
- int logchanges;
- char **attrs;
- } referint_config;
- Slapi_RWLock *config_rwlock = NULL;
- /* function prototypes */
- int referint_postop_init( Slapi_PBlock *pb );
- int referint_postop_del( Slapi_PBlock *pb );
- int referint_postop_modrdn( Slapi_PBlock *pb );
- int referint_postop_start( Slapi_PBlock *pb);
- int referint_postop_close( Slapi_PBlock *pb);
- int update_integrity(Slapi_DN *sDN, char *newrDN, Slapi_DN *newsuperior, int logChanges);
- int GetNextLine(char *dest, int size_dest, PRFileDesc *stream);
- int my_fgetc(PRFileDesc *stream);
- void referint_thread_func(void *arg);
- void writeintegritylog(Slapi_PBlock *pb, char *logfilename, Slapi_DN *sdn, char *newrdn, Slapi_DN *newsuperior, Slapi_DN *requestorsdn);
- int load_config(Slapi_PBlock *pb, Slapi_Entry *config_entry, int apply);
- int referint_get_delay(void);
- int referint_get_logchanges(void);
- char *referint_get_logfile(void);
- char **referint_get_attrs(void);
- int referint_postop_modify(Slapi_PBlock *pb);
- int referint_validate_config(Slapi_PBlock *pb);
- static int referint_preop_init(Slapi_PBlock *pb);
- void referint_set_config_area(Slapi_DN *dn);
- Slapi_DN *referint_get_config_area(void);
- void referint_set_plugin_area(Slapi_DN *sdn);
- Slapi_DN *referint_get_plugin_area(void);
- int referint_sdn_config_cmp(Slapi_DN *sdn);
- void referint_get_config(int *delay, int *logchanges, char **logfile);
- /* global thread control stuff */
- static PRLock *referint_mutex = NULL;
- static PRThread *referint_tid = NULL;
- static PRLock *keeprunning_mutex = NULL;
- static PRCondVar *keeprunning_cv = NULL;
- static int keeprunning = 0;
- static referint_config *config = NULL;
- static Slapi_DN* _ConfigAreaDN = NULL;
- static Slapi_DN* _pluginDN = NULL;
- static Slapi_PluginDesc pdesc = { "referint", VENDOR, DS_PACKAGE_VERSION, "referential integrity plugin" };
- static int allow_repl = 0;
- static Slapi_DN **plugin_EntryScope = NULL;
- static Slapi_DN *plugin_ExcludeEntryScope = NULL;
- static Slapi_DN *plugin_ContainerScope = NULL;
- static void* referint_plugin_identity = NULL;
- static int use_txn = 0;
- static int premodfn = SLAPI_PLUGIN_PRE_MODIFY_FN;
- static void
- referint_lock(void)
- {
- if (use_txn) { /* no lock if betxn is enabled */
- return;
- }
- if (NULL == referint_mutex) {
- referint_mutex = PR_NewLock();
- }
- if (referint_mutex) {
- PR_Lock(referint_mutex);
- }
- }
- static void
- referint_unlock(void)
- {
- if (use_txn) { /* no lock if betxn is enabled */
- return;
- }
- if (referint_mutex) {
- PR_Unlock(referint_mutex);
- }
- }
- void
- referint_set_config_area(Slapi_DN *dn)
- {
- slapi_rwlock_wrlock(config_rwlock);
- slapi_sdn_free(&_ConfigAreaDN);
- _ConfigAreaDN = slapi_sdn_dup(dn);
- slapi_rwlock_unlock(config_rwlock);
- }
- /*
- * No need to lock here, because this only called from referint_sdn_config_cmp()
- * which does take the lock.
- */
- Slapi_DN *
- referint_get_config_area(void)
- {
- return _ConfigAreaDN;
- }
- /* no locking needed for the plugin DN because it is set at initialization */
- void
- referint_set_plugin_area(Slapi_DN *sdn)
- {
- slapi_sdn_free(&_pluginDN);
- _pluginDN = slapi_sdn_dup(sdn);
- }
- Slapi_DN *
- referint_get_plugin_area(void)
- {
- return _pluginDN;
- }
- int
- referint_postop_init( Slapi_PBlock *pb )
- {
- Slapi_Entry *plugin_entry = NULL;
- char *plugin_type = NULL;
- int delfn = SLAPI_PLUGIN_POST_DELETE_FN;
- int mdnfn = SLAPI_PLUGIN_POST_MODRDN_FN;
- int modfn = SLAPI_PLUGIN_POST_MODIFY_FN; /* for config changes */
- char *preop_plugin_type = "preoperation";
- /*
- * Get plugin identity and stored it for later use.
- * Used for internal operations.
- */
- slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &referint_plugin_identity);
- PR_ASSERT (referint_plugin_identity);
- if((config = (referint_config *)slapi_ch_calloc (1, sizeof (referint_config))) == NULL){
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM, "referint_postop_init - Failed to "
- "allocate configuration\n" );
- return ( -1 );
- }
- /* get the args */
- 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"))
- {
- delfn = SLAPI_PLUGIN_BE_TXN_POST_DELETE_FN;
- mdnfn = SLAPI_PLUGIN_BE_TXN_POST_MODRDN_FN;
- modfn = SLAPI_PLUGIN_BE_TXN_POST_MODIFY_FN;
- preop_plugin_type = "betxnpreoperation";
- premodfn = SLAPI_PLUGIN_BE_TXN_PRE_MODIFY_FN;
- use_txn = 1;
- }
- slapi_ch_free_string(&plugin_type);
- if(plugin_entry){
- char *plugin_attr_value;
- char **plugin_attr_values;
- plugin_attr_value = slapi_entry_attr_get_charptr(plugin_entry, "nsslapd-pluginAllowReplUpdates");
- if(plugin_attr_value && strcasecmp(plugin_attr_value,"on")==0){
- allow_repl = 1;
- }
- slapi_ch_free_string(&plugin_attr_value);
- plugin_attr_values = slapi_entry_attr_get_charray(plugin_entry, "nsslapd-pluginEntryScope");
- if(plugin_attr_values) {
- int i,j=0;;
- for (i=0; plugin_attr_values[i];i++);
- plugin_EntryScope = (Slapi_DN **)slapi_ch_calloc(sizeof(Slapi_DN *),i+1);
- for (i=0; plugin_attr_values[i];i++) {
- if (slapi_dn_syntax_check(NULL, plugin_attr_values[i], 1) == 1) {
- slapi_log_error(SLAPI_LOG_WARNING, REFERINT_PLUGIN_SUBSYSTEM,
- "referint_postop_init - Ignoring invalid DN used as plugin entry scope: [%s]\n",
- plugin_attr_values[i]);
- slapi_ch_free_string(&plugin_attr_values[i]);
- } else {
- plugin_EntryScope[j++] = slapi_sdn_new_dn_passin(plugin_attr_values[i]);
- }
- }
- slapi_ch_free((void**)&plugin_attr_values);
- }
- plugin_attr_value = slapi_entry_attr_get_charptr(plugin_entry, "nsslapd-pluginExcludeEntryScope");
- if(plugin_attr_value) {
- if (slapi_dn_syntax_check(NULL, plugin_attr_value, 1) == 1) {
- slapi_log_error(SLAPI_LOG_WARNING, REFERINT_PLUGIN_SUBSYSTEM,
- "referint_postop_init - Ignoring invalid DN used as plugin exclude entry scope: [%s]\n",
- plugin_attr_value);
- slapi_ch_free_string(&plugin_attr_value);
- } else {
- plugin_ExcludeEntryScope = slapi_sdn_new_dn_passin(plugin_attr_value);
- }
- }
- plugin_attr_value = slapi_entry_attr_get_charptr(plugin_entry, "nsslapd-pluginContainerScope");
- if(plugin_attr_value) {
- if (slapi_dn_syntax_check(NULL, plugin_attr_value, 1) == 1) {
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
- "referint_postop_init - Ignoring invalid DN used as plugin container scope: [%s]\n",
- plugin_attr_value);
- slapi_ch_free_string(&plugin_attr_value);
- } else {
- plugin_ContainerScope = slapi_sdn_new_dn_passin(plugin_attr_value);
- }
- }
- referint_set_plugin_area(slapi_entry_get_sdn(plugin_entry));
- }
- 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, delfn, (void *) referint_postop_del ) != 0 ||
- slapi_pblock_set( pb, mdnfn, (void *) referint_postop_modrdn ) != 0 ||
- slapi_pblock_set( pb, modfn, (void *) (void *)referint_postop_modify ) != 0 ||
- slapi_pblock_set( pb, SLAPI_PLUGIN_START_FN, (void *) referint_postop_start ) != 0 ||
- slapi_pblock_set( pb, SLAPI_PLUGIN_CLOSE_FN, (void *) referint_postop_close ) != 0)
- {
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM, "referint_postop_init - Failed\n" );
- return( -1 );
- }
- /*
- * Setup the preop plugin for config validation
- */
- if (slapi_register_plugin(preop_plugin_type, /* op type */
- 1, /* Enabled */
- "referint_preop_init", /* this function desc */
- referint_preop_init, /* init func */
- REFERINT_PREOP_DESC, /* plugin desc */
- NULL, /* ? */
- referint_plugin_identity /* access control */))
- {
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM, "referint_postop_init - Preop registration failed\n" );
- return ( -1 );
- }
- return( 0 );
- }
- /*
- * referint-update-delay: 0
- * referint-logfile: /var/log/dirsrv/slapd-localhost/referint
- * referint-logchanges: 0
- * referint-membership-attr: member
- * referint-membership-attr: uniquemember
- * referint-membership-attr: owner
- * referint-membership-attr: seeAlso
- *
- *
- * Need to lock this!
- */
- int
- load_config(Slapi_PBlock *pb, Slapi_Entry *config_entry, int apply)
- {
- referint_config *tmp_config = NULL;
- char *value = NULL;
- char **attrs = NULL;
- char **argv = NULL;
- int new_config_present = 0;
- int argc = 0;
- int rc = SLAPI_PLUGIN_SUCCESS;
- if(config_entry == NULL){
- return rc;
- }
- slapi_rwlock_wrlock(config_rwlock);
- if(config == NULL){
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
- "load_config: config is NULL\n" );
- rc = SLAPI_PLUGIN_FAILURE;
- goto done;
- }
- if((tmp_config = (referint_config *)slapi_ch_calloc (1, sizeof (referint_config))) == NULL){
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM, "load_config - Failed to "
- "allocate configuration\n" );
- rc = SLAPI_PLUGIN_FAILURE;
- goto done;
- } else {
- /* set these to -1 for config validation */
- tmp_config->delay = -1;
- tmp_config->logchanges = -1;
- }
- if((value = slapi_entry_attr_get_charptr(config_entry, REFERINT_ATTR_DELAY))){
- tmp_config->delay = atoi(value);
- slapi_ch_free_string(&value);
- new_config_present = 1;
- }
- if((value = slapi_entry_attr_get_charptr(config_entry, REFERINT_ATTR_LOGFILE))){
- tmp_config->logfile = value;
- new_config_present = 1;
- }
- if((value = slapi_entry_attr_get_charptr(config_entry, REFERINT_ATTR_LOGCHANGES))){
- tmp_config->logchanges = atoi(value);
- slapi_ch_free_string(&value);
- new_config_present = 1;
- }
- if((attrs = slapi_entry_attr_get_charray(config_entry, REFERINT_ATTR_MEMBERSHIP))){
- tmp_config->attrs = attrs;
- new_config_present = 1;
- }
- if(new_config_present){
- /* Verify we have everything we need */
- if(tmp_config->delay == -1){
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM, "load_config - Plugin configuration is missing %s\n",
- REFERINT_ATTR_DELAY);
- rc = SLAPI_PLUGIN_FAILURE;
- } else if (!tmp_config->logfile){
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM, "load_config - Plugin configuration is missing %s\n",
- REFERINT_ATTR_LOGFILE);
- rc = SLAPI_PLUGIN_FAILURE;
- } else if (tmp_config->logchanges == -1){
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM, "load_config - Plugin configuration is missing %s\n",
- REFERINT_ATTR_LOGCHANGES);
- rc = SLAPI_PLUGIN_FAILURE;
- } else if (!tmp_config->attrs){
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM, "load_config - Plugin configuration is missing %s\n",
- REFERINT_ATTR_MEMBERSHIP);
- rc = SLAPI_PLUGIN_FAILURE;
- }
- } else{
- /*
- * We are using the old plugin arg configuration, get the args
- */
- if ( slapi_pblock_get( pb, SLAPI_PLUGIN_ARGC, &argc ) != 0) {
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
- "load_config - Failed to get argc\n" );
- rc = SLAPI_PLUGIN_FAILURE;
- goto done;
- }
- if ( slapi_pblock_get( pb, SLAPI_PLUGIN_ARGV, &argv ) != 0) {
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
- "load_config - Failed to get argv\n" );
- rc = SLAPI_PLUGIN_FAILURE;
- goto done;
- }
- if(argv == NULL){
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
- "load_config - args are NULL\n" );
- rc = SLAPI_PLUGIN_FAILURE;
- goto done;
- }
- /*
- * Load the args and set the config struct
- */
- if (argc >= 3) {
- int i;
- tmp_config->delay = atoi(argv[0]);
- tmp_config->logfile = slapi_ch_strdup(argv[1]);
- tmp_config->logchanges = atoi(argv[2]);
- for(i = 3; argv[i] != NULL; i++){
- slapi_ch_array_add(&tmp_config->attrs, slapi_ch_strdup(argv[i]));
- }
- } else {
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
- "load_config - Insufficient arguments supplied\n" );
- rc = SLAPI_PLUGIN_FAILURE;
- goto done;
- }
- }
- done:
- if(apply && rc == SLAPI_PLUGIN_SUCCESS){
- slapi_ch_free_string(&config->logfile);
- slapi_ch_array_free(config->attrs);
- slapi_ch_free((void **)&config);
- config = tmp_config;
- } else if(tmp_config){
- slapi_ch_free_string(&tmp_config->logfile);
- slapi_ch_array_free(tmp_config->attrs);
- slapi_ch_free((void **)&tmp_config);
- }
- slapi_rwlock_unlock(config_rwlock);
- return rc;
- }
- int
- referint_postop_modify(Slapi_PBlock *pb)
- {
- Slapi_Entry *entry = NULL, *e = NULL;
- Slapi_Entry *config_e = NULL;
- Slapi_DN *config_sdn = NULL;
- Slapi_DN *sdn = NULL;
- char *config_area = NULL;
- int result = 0;
- int rc = SLAPI_PLUGIN_SUCCESS;
- /* check if we are updating the shared config entry */
- slapi_pblock_get(pb, SLAPI_TARGET_SDN, &sdn);
- slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &entry);
- if (referint_sdn_config_cmp(sdn) == 0 && slapi_sdn_compare(sdn, referint_get_plugin_area()))
- {
- if( SLAPI_PLUGIN_FAILURE == load_config(pb, entry, 1)){
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM, "referint_postop_modify - "
- "Failed to update configuration.\n");
- return SLAPI_PLUGIN_FAILURE;
- }
- } else if (slapi_sdn_compare(sdn, referint_get_plugin_area()) == 0){
- /*
- * Check if the plugin config area is set(verify it and load its config),
- * otherwise reload the plugin entry config
- */
- if((config_area = slapi_entry_attr_get_charptr(entry, SLAPI_PLUGIN_SHARED_CONFIG_AREA))){
- rc = slapi_dn_syntax_check(pb, config_area, 1);
- if (rc) { /* syntax check failed */
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM, "referint_postop_modify - "
- "%s does not contain a valid DN (%s)\n",
- SLAPI_PLUGIN_SHARED_CONFIG_AREA, config_area);
- rc = LDAP_INVALID_DN_SYNTAX;
- goto bail;
- }
- config_sdn = slapi_sdn_new_dn_byval(config_area);
- result = slapi_search_internal_get_entry(config_sdn, NULL, &e, referint_plugin_identity);
- if (LDAP_SUCCESS != result) {
- if (result == LDAP_NO_SUCH_OBJECT) {
- /* log an error and use the plugin entry for the config */
- slapi_log_error(SLAPI_LOG_PLUGIN, REFERINT_PLUGIN_SUBSYSTEM,
- "referint_postop_modify - Config entry \"%s\" does "
- "not exist.\n", config_area);
- rc = LDAP_OPERATIONS_ERROR;
- goto bail;
- }
- } else {
- if(e){
- config_e = e;
- } else {
- slapi_log_error(SLAPI_LOG_PLUGIN, REFERINT_PLUGIN_SUBSYSTEM,
- "referint_postop_modify - Config entry \"%s\" was not located.\n", config_area);
- rc = LDAP_OPERATIONS_ERROR;
- goto bail;
- }
- }
- } else {
- config_e = entry;
- }
- if(load_config(pb, config_e, 1) != LDAP_SUCCESS){
- rc = LDAP_UNWILLING_TO_PERFORM;
- goto bail;
- }
- referint_set_config_area(slapi_entry_get_sdn(config_e));
- }
- bail:
- slapi_ch_free_string(&config_area);
- slapi_sdn_free(&config_sdn);
- slapi_entry_free(e);
- return rc;
- }
- int
- referint_get_delay(void)
- {
- int delay;
- slapi_rwlock_rdlock(config_rwlock);
- delay = config->delay;
- slapi_rwlock_unlock(config_rwlock);
- return delay;
- }
- int
- referint_get_logchanges(void)
- {
- int log_changes;
- slapi_rwlock_rdlock(config_rwlock);
- log_changes = config->logchanges;
- slapi_rwlock_unlock(config_rwlock);
- return log_changes;
- }
- char *
- referint_get_logfile(void)
- {
- char *log_file;
- slapi_rwlock_rdlock(config_rwlock);
- log_file = slapi_ch_strdup(config->logfile);
- slapi_rwlock_unlock(config_rwlock);
- return log_file;
- }
- void
- referint_get_config(int *delay, int *logchanges, char **logfile)
- {
- slapi_rwlock_rdlock(config_rwlock);
- if(delay){
- *delay = config->delay;
- }
- if(logchanges){
- *logchanges = config->logchanges;
- }
- if(logfile){
- *logfile = slapi_ch_strdup(config->logfile);
- }
- slapi_rwlock_unlock(config_rwlock);
- }
- /*
- * might need to find an alternate option instead of copying
- */
- char **
- referint_get_attrs(void)
- {
- char **attrs = NULL;
- slapi_rwlock_rdlock(config_rwlock);
- attrs = slapi_ch_array_dup(config->attrs);
- slapi_rwlock_unlock(config_rwlock);
- return attrs;
- }
- int
- referint_sdn_config_cmp(Slapi_DN *sdn)
- {
- int rc = 0;
- slapi_rwlock_rdlock(config_rwlock);
- rc = slapi_sdn_compare(sdn, referint_get_config_area());
- slapi_rwlock_unlock(config_rwlock);
- return rc;
- }
- int
- referint_sdn_in_container_scope(Slapi_DN *sdn)
- {
- if (plugin_ContainerScope == NULL) {
- return(1);
- } else {
- return(slapi_sdn_issuffix(sdn, plugin_ContainerScope));
- }
- }
- int
- referint_sdn_in_entry_scope(Slapi_DN *sdn)
- {
- int rc = 0;
- if (plugin_ExcludeEntryScope && slapi_sdn_issuffix(sdn, plugin_ExcludeEntryScope))
- return (0);
- if (plugin_EntryScope == NULL) {
- /* no scope defined, all sdns match */
- return(1);
- } else {
- int i = 0;
- while (plugin_EntryScope[i]) {
- if (slapi_sdn_issuffix(sdn, plugin_EntryScope[i]) ) {
- rc = 1;
- break;
- } else {
- i++;
- }
- }
- }
- return (rc);
- }
- int
- referint_postop_del( Slapi_PBlock *pb )
- {
- Slapi_DN *sdn = NULL;
- char *logfile = NULL;
- int delay;
- int logChanges=0;
- int isrepop = 0;
- int oprc;
- int rc = SLAPI_PLUGIN_SUCCESS;
- if ( slapi_pblock_get( pb, SLAPI_IS_REPLICATED_OPERATION, &isrepop ) != 0 ||
- slapi_pblock_get( pb, SLAPI_DELETE_TARGET_SDN, &sdn ) != 0 ||
- slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &oprc) != 0)
- {
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
- "referint_postop_del - Could not get parameters\n" );
- return SLAPI_PLUGIN_FAILURE;
- }
- /*
- * This plugin should only execute if the delete was successful
- * and this is not a replicated op(unless its allowed)
- */
- if(oprc != 0 || (isrepop && !allow_repl)){
- return SLAPI_PLUGIN_SUCCESS;
- }
- referint_get_config(&delay, &logChanges, NULL);
- if(delay == -1){
- /* integrity updating is off */
- rc = SLAPI_PLUGIN_SUCCESS;
- } else if(delay == 0){ /* no delay */
- /* call function to update references to entry */
- if (referint_sdn_in_entry_scope(sdn)) {
- rc = update_integrity(sdn, NULL, NULL, logChanges);
- }
- } else {
- /* write the entry to integrity log */
- logfile = referint_get_logfile();
- writeintegritylog(pb, logfile, sdn, NULL, NULL, NULL /* slapi_get_requestor_sdn(pb) */);
- rc = SLAPI_PLUGIN_SUCCESS;
- }
- slapi_ch_free_string(&logfile);
- return( rc );
- }
- int
- referint_postop_modrdn( Slapi_PBlock *pb )
- {
- Slapi_DN *sdn = NULL;
- Slapi_DN *newsuperior;
- char *logfile = NULL;
- char *newrdn;
- int oprc;
- int rc = SLAPI_PLUGIN_SUCCESS;
- int delay;
- int logChanges=0;
- int isrepop = 0;
- if ( slapi_pblock_get( pb, SLAPI_IS_REPLICATED_OPERATION, &isrepop ) != 0 ||
- slapi_pblock_get( pb, SLAPI_MODRDN_TARGET_SDN, &sdn ) != 0 ||
- slapi_pblock_get( pb, SLAPI_MODRDN_NEWRDN, &newrdn ) != 0 ||
- slapi_pblock_get( pb, SLAPI_MODRDN_NEWSUPERIOR_SDN, &newsuperior ) != 0 ||
- slapi_pblock_get( pb, SLAPI_PLUGIN_OPRETURN, &oprc) != 0 )
- {
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
- "referint_postop_modrdn - Could not get parameters\n" );
- return SLAPI_PLUGIN_FAILURE;
- }
- /*
- * This plugin should only execute if the delete was successful
- * and this is not a replicated op (unless its allowed)
- */
- if(oprc != 0 || (isrepop && !allow_repl)){
- return SLAPI_PLUGIN_SUCCESS;
- }
- referint_get_config(&delay, &logChanges, NULL);
- if(delay == -1){
- /* integrity updating is off */
- rc = SLAPI_PLUGIN_SUCCESS;
- } else if(delay == 0){ /* no delay */
- /* call function to update references to entry */
- if (!plugin_EntryScope && !plugin_ExcludeEntryScope) {
- /* no scope defined, default always process referint */
- rc = update_integrity(sdn, newrdn, newsuperior, logChanges);
- } else {
- const char *newsuperiordn = slapi_sdn_get_dn(newsuperior);
- if ( (newsuperiordn == NULL && referint_sdn_in_entry_scope(sdn)) ||
- ( newsuperiordn && referint_sdn_in_entry_scope(newsuperior)))
- {
- /*
- * It is a modrdn inside the scope or into the scope,
- * process normal modrdn
- */
- rc = update_integrity(sdn, newrdn, newsuperior, logChanges);
- } else if (referint_sdn_in_entry_scope(sdn)) {
- /* the entry is moved out of scope, treat as delete */
- rc = update_integrity(sdn, NULL, NULL, logChanges);
- }
- }
- } else {
- /* write the entry to integrity log */
- logfile = referint_get_logfile();
- writeintegritylog(pb, logfile, sdn, newrdn, newsuperior, NULL /* slapi_get_requestor_sdn(pb) */);
- rc = SLAPI_PLUGIN_SUCCESS;
- }
- slapi_ch_free_string(&logfile);
- return( rc );
- }
- int isFatalSearchError(int search_result)
- {
- /* Make sure search result is fatal
- * Some conditions that happen quite often are not fatal
- * for example if you have two suffixes and one is null, you will always
- * get no such object, however this is not a fatal error.
- * Add other conditions to the if statement as they are found
- */
- switch(search_result) {
- case LDAP_REFERRAL:
- case LDAP_NO_SUCH_OBJECT: return 0 ;
- }
- return 1;
- }
- static int
- _do_modify(Slapi_PBlock *mod_pb, Slapi_DN *entrySDN, LDAPMod **mods)
- {
- int rc = 0;
- slapi_pblock_init(mod_pb);
- if(allow_repl){
- /* Must set as a replicated operation */
- slapi_modify_internal_set_pb_ext(mod_pb, entrySDN, mods, NULL, NULL,
- referint_plugin_identity, OP_FLAG_REPLICATED);
- } else {
- slapi_modify_internal_set_pb_ext(mod_pb, entrySDN, mods, NULL, NULL,
- referint_plugin_identity, 0);
- }
- slapi_modify_internal_pb(mod_pb);
- slapi_pblock_get(mod_pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
-
- return rc;
- }
- /*
- * update one attribute value per _do_modify
- */
- static int
- _update_one_per_mod(Slapi_DN *entrySDN, /* DN of the searched entry */
- Slapi_Attr *attr, /* referred attribute */
- char *attrName,
- Slapi_DN *origDN, /* original DN that was modified */
- char *newRDN, /* new RDN from modrdn */
- const char *newsuperior, /* new superior from modrdn */
- Slapi_PBlock *mod_pb)
- {
- LDAPMod attribute1, attribute2;
- LDAPMod *list_of_mods[3];
- char *values_del[2];
- char *values_add[2];
- char *newDN = NULL;
- char **dnParts = NULL;
- char *sval = NULL;
- char *newvalue = NULL;
- char *p = NULL;
- size_t dnlen = 0;
- int rc = 0;
- if (NULL == newRDN && NULL == newsuperior) {
- /* in delete mode */
- /* delete old dn so set that up */
- values_del[0] = (char *)slapi_sdn_get_dn(origDN);
- values_del[1] = NULL;
- attribute1.mod_type = attrName;
- attribute1.mod_op = LDAP_MOD_DELETE;
- attribute1.mod_values = values_del;
- list_of_mods[0] = &attribute1;
- /* terminate list of mods. */
- list_of_mods[1] = NULL;
- rc = _do_modify(mod_pb, entrySDN, list_of_mods);
- if (rc) {
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
- "_update_one_per_mod - Entry %s: deleting \"%s: %s\" failed (%d)"
- "\n", slapi_sdn_get_dn(entrySDN), attrName, slapi_sdn_get_dn(origDN), rc);
- }
- } else {
- /* in modrdn mode */
- const char *superior = NULL;
- int nval = 0;
- Slapi_Value *v = NULL;
- if (NULL == origDN) {
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
- "_update_one_per_mod - NULL dn was passed\n");
- goto bail;
- }
- /* need to put together rdn into a dn */
- dnParts = slapi_ldap_explode_dn( slapi_sdn_get_dn(origDN), 0 );
- if (NULL == dnParts) {
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
- "_update_one_per_mod - Failed to explode dn %s\n",
- slapi_sdn_get_dn(origDN));
- goto bail;
- }
- if (NULL == newRDN) {
- newRDN = dnParts[0];
- }
- if (newsuperior) {
- superior = newsuperior;
- } else {
- /* do not free superior */
- superior = slapi_dn_find_parent(slapi_sdn_get_dn(origDN));
- }
- /* newRDN and superior are already normalized. */
- newDN = slapi_ch_smprintf("%s,%s", newRDN, superior);
- slapi_dn_ignore_case(newDN);
- /*
- * Compare the modified dn with the value of
- * the target attribute of referint to find out
- * the modified dn is the ancestor (case 2) or
- * the value itself (case 1).
- *
- * E.g.,
- * (case 1)
- * modrdn: uid=A,ou=B,o=C --> uid=A',ou=B',o=C
- * (origDN) (newDN)
- * member: uid=A,ou=B,ou=C --> uid=A',ou=B',ou=C
- * (sval) (newDN)
- *
- * (case 2)
- * modrdn: ou=B,o=C --> ou=B',o=C
- * (origDN) (newDN)
- * member: uid=A,ou=B,ou=C --> uid=A,ou=B',ou=C
- * (sval) (sval' + newDN)
- */
- for (nval = slapi_attr_first_value(attr, &v); nval != -1;
- nval = slapi_attr_next_value(attr, nval, &v)) {
- p = NULL;
- dnlen = 0;
- /* DN syntax, which should be a string */
- sval = slapi_ch_strdup(slapi_value_get_string(v));
- rc = slapi_dn_normalize_case_ext(sval, 0, &p, &dnlen);
- if (rc == 0) { /* sval is passed in; not terminated */
- *(p + dnlen) = '\0';
- sval = p;
- } else if (rc > 0) {
- slapi_ch_free_string(&sval);
- sval = p;
- }
- /* else: (rc < 0) Ignore the DN normalization error for now. */
- p = PL_strstr(sval, slapi_sdn_get_ndn(origDN));
- if (p == sval) {
- /* (case 1) */
- values_del[0] = sval;
- values_del[1] = NULL;
- attribute1.mod_type = attrName;
- attribute1.mod_op = LDAP_MOD_DELETE;
- attribute1.mod_values = values_del;
- list_of_mods[0] = &attribute1;
- values_add[0] = newDN;
- values_add[1] = NULL;
- attribute2.mod_type = attrName;
- attribute2.mod_op = LDAP_MOD_ADD;
- attribute2.mod_values = values_add;
- list_of_mods[1] = &attribute2;
- list_of_mods[2] = NULL;
- rc = _do_modify(mod_pb, entrySDN, list_of_mods);
- if (rc) {
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
- "_update_one_per_mod - Entry %s: replacing \"%s: %s\" "
- "with \"%s: %s\" failed (%d)\n",
- slapi_sdn_get_dn(entrySDN), attrName,
- slapi_sdn_get_dn(origDN), attrName, newDN, rc);
- }
- } else if (p) {
- char bak;
- /* (case 2) */
- values_del[0] = sval;
- values_del[1] = NULL;
- attribute1.mod_type = attrName;
- attribute1.mod_op = LDAP_MOD_DELETE;
- attribute1.mod_values = values_del;
- list_of_mods[0] = &attribute1;
- bak = *p;
- *p = '\0';
- /* newRDN and superior are already normalized. */
- newvalue = slapi_ch_smprintf("%s%s", sval, newDN);
- *p = bak;
- values_add[0]=newvalue;
- values_add[1]=NULL;
- attribute2.mod_type = attrName;
- attribute2.mod_op = LDAP_MOD_ADD;
- attribute2.mod_values = values_add;
- list_of_mods[1] = &attribute2;
- list_of_mods[2] = NULL;
- rc = _do_modify(mod_pb, entrySDN, list_of_mods);
- if (rc) {
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
- "_update_one_per_mod - Entry %s: replacing \"%s: %s\" "
- "with \"%s: %s\" failed (%d)\n",
- slapi_sdn_get_dn(entrySDN), attrName, sval, attrName, newvalue, rc);
- }
- slapi_ch_free_string(&newvalue);
- }
- /* else: value does not include the modified DN. Ignore it. */
- slapi_ch_free_string(&sval);
- }
- /* cleanup memory allocated for dnParts and newDN */
- if (dnParts){
- slapi_ldap_value_free(dnParts);
- dnParts = NULL;
- }
- slapi_ch_free_string(&newDN);
- }
- bail:
- return rc;
- }
- /*
- * update multiple attribute values per _do_modify
- */
- static int
- _update_all_per_mod(Slapi_DN *entrySDN, /* DN of the searched entry */
- Slapi_Attr *attr, /* referred attribute */
- char *attrName,
- Slapi_DN *origDN, /* original DN that was modified */
- char *newRDN, /* new RDN from modrdn */
- const char *newsuperior, /* new superior from modrdn */
- Slapi_PBlock *mod_pb)
- {
- Slapi_Mods *smods = NULL;
- char *newDN = NULL;
- char **dnParts = NULL;
- char *sval = NULL;
- char *newvalue = NULL;
- char *p = NULL;
- size_t dnlen = 0;
- int rc = 0;
- int nval = 0;
- slapi_attr_get_numvalues(attr, &nval);
- if (NULL == newRDN && NULL == newsuperior) {
- /* in delete mode */
- LDAPMod *mods[2];
- char *values_del[2];
- LDAPMod attribute1;
- /* delete old dn so set that up */
- values_del[0] = (char *)slapi_sdn_get_dn(origDN);
- values_del[1] = NULL;
- attribute1.mod_type = attrName;
- attribute1.mod_op = LDAP_MOD_DELETE;
- attribute1.mod_values = values_del;
- mods[0] = &attribute1;
- /* terminate list of mods. */
- mods[1] = NULL;
- rc = _do_modify(mod_pb, entrySDN, mods);
- if (rc) {
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
- "_update_all_per_mod - Entry %s: deleting \"%s: %s\" failed (%d)"
- "\n", slapi_sdn_get_dn(entrySDN), attrName, slapi_sdn_get_dn(origDN), rc);
- }
- } else {
- /* in modrdn mode */
- const char *superior = NULL;
- int nval = 0;
- Slapi_Value *v = NULL;
- if (NULL == origDN) {
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
- "_update_all_per_mod - NULL dn was passed\n");
- goto bail;
- }
- /* need to put together rdn into a dn */
- dnParts = slapi_ldap_explode_dn( slapi_sdn_get_dn(origDN), 0 );
- if (NULL == dnParts) {
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
- "_update_all_per_mod - Failed to explode dn %s\n",
- slapi_sdn_get_dn(origDN));
- goto bail;
- }
- if (NULL == newRDN) {
- newRDN = dnParts[0];
- }
- if (newsuperior) {
- superior = newsuperior;
- } else {
- /* do not free superior */
- superior = slapi_dn_find_parent(slapi_sdn_get_dn(origDN));
- }
- /* newRDN and superior are already normalized. */
- newDN = slapi_ch_smprintf("%s,%s", newRDN, superior);
- slapi_dn_ignore_case(newDN);
- /*
- * Compare the modified dn with the value of
- * the target attribute of referint to find out
- * the modified dn is the ancestor (case 2) or
- * the value itself (case 1).
- *
- * E.g.,
- * (case 1)
- * modrdn: uid=A,ou=B,o=C --> uid=A',ou=B',o=C
- * (origDN) (newDN)
- * member: uid=A,ou=B,ou=C --> uid=A',ou=B',ou=C
- * (sval) (newDN)
- *
- * (case 2)
- * modrdn: ou=B,o=C --> ou=B',o=C
- * (origDN) (newDN)
- * member: uid=A,ou=B,ou=C --> uid=A,ou=B',ou=C
- * (sval) (sval' + newDN)
- */
- slapi_attr_get_numvalues(attr, &nval);
- smods = slapi_mods_new();
- slapi_mods_init(smods, 2 * nval + 1);
- for (nval = slapi_attr_first_value(attr, &v);
- nval != -1;
- nval = slapi_attr_next_value(attr, nval, &v)) {
- p = NULL;
- dnlen = 0;
- /* DN syntax, which should be a string */
- sval = slapi_ch_strdup(slapi_value_get_string(v));
- rc = slapi_dn_normalize_case_ext(sval, 0, &p, &dnlen);
- if (rc == 0) { /* sval is passed in; not terminated */
- *(p + dnlen) = '\0';
- sval = p;
- } else if (rc > 0) {
- slapi_ch_free_string(&sval);
- sval = p;
- }
- /* else: (rc < 0) Ignore the DN normalization error for now. */
- p = PL_strstr(sval, slapi_sdn_get_ndn(origDN));
- if (p == sval) {
- /* (case 1) */
- slapi_mods_add_string(smods, LDAP_MOD_DELETE, attrName, sval);
- slapi_mods_add_string(smods, LDAP_MOD_ADD, attrName, newDN);
- } else if (p) {
- /* (case 2) */
- slapi_mods_add_string(smods, LDAP_MOD_DELETE, attrName, sval);
- *p = '\0';
- newvalue = slapi_ch_smprintf("%s%s", sval, newDN);
- slapi_mods_add_string(smods, LDAP_MOD_ADD, attrName, newvalue);
- slapi_ch_free_string(&newvalue);
- }
- /* else: value does not include the modified DN. Ignore it. */
- slapi_ch_free_string(&sval);
- }
- rc = _do_modify(mod_pb, entrySDN, slapi_mods_get_ldapmods_byref(smods));
- if (rc) {
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
- "_update_all_per_mod - Entry %s failed (%d)\n",
- slapi_sdn_get_dn(entrySDN), rc);
- }
- /* cleanup memory allocated for dnParts and newDN */
- if (dnParts){
- slapi_ldap_value_free(dnParts);
- dnParts = NULL;
- }
- slapi_ch_free_string(&newDN);
- slapi_mods_free(&smods);
- }
- bail:
- return rc;
- }
- int
- update_integrity(Slapi_DN *origSDN,
- char *newrDN, Slapi_DN *newsuperior,
- int logChanges)
- {
- Slapi_PBlock *search_result_pb = NULL;
- Slapi_PBlock *mod_pb = slapi_pblock_new();
- Slapi_Entry **search_entries = NULL;
- Slapi_DN *sdn = NULL;
- Slapi_Attr *attr = NULL;
- void *node = NULL;
- const char *origDN = slapi_sdn_get_dn(origSDN);
- const char *search_base = NULL;
- char *attrName = NULL;
- char *filter = NULL;
- char *attrs[2];
- char **membership_attrs = NULL;
- int search_result;
- int nval = 0;
- int i, j;
- int rc = SLAPI_PLUGIN_SUCCESS;
- membership_attrs = referint_get_attrs();
- /*
- * For now, just putting attributes to keep integrity on in conf file,
- * until resolve the other timing mode issue
- */
- search_result_pb = slapi_pblock_new();
- /* Search each namingContext in turn
- * or use the defined scope(s)
- */
- if (plugin_ContainerScope) {
- sdn = plugin_ContainerScope;
- } else {
- sdn = slapi_get_first_suffix( &node, 0 );
- }
- while (sdn)
- {
- Slapi_Backend *be = slapi_be_select(sdn);
- search_base = slapi_sdn_get_dn( sdn );
- for(i = 0; membership_attrs[i] != NULL; i++){
- if(newrDN){
- /* we need to check the children of the old dn, so use a wildcard */
- filter = slapi_filter_sprintf("(%s=*%s%s)", membership_attrs[i], ESC_NEXT_VAL, origDN);
- } else {
- filter = slapi_filter_sprintf("(%s=%s%s)", membership_attrs[i], ESC_NEXT_VAL, origDN);
- }
- if ( filter ) {
- /* Need only the current attribute and its subtypes */
- attrs[0] = membership_attrs[i];
- attrs[1] = NULL;
- /* Use new search API */
- slapi_pblock_init(search_result_pb);
- slapi_pblock_set(search_result_pb, SLAPI_BACKEND, be);
- slapi_search_internal_set_pb(search_result_pb, search_base,
- LDAP_SCOPE_SUBTREE, filter, attrs, 0 /* attrs only */,
- NULL, NULL, referint_plugin_identity, 0);
- slapi_search_internal_pb(search_result_pb);
-
- slapi_pblock_get( search_result_pb, SLAPI_PLUGIN_INTOP_RESULT, &search_result);
- /* if search successfull then do integrity update */
- if(search_result == LDAP_SUCCESS)
- {
- slapi_pblock_get(search_result_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
- &search_entries);
- for(j = 0; search_entries[j] != NULL; j++){
- attr = NULL;
- attrName = NULL;
- /*
- * Loop over all the attributes of the entry and search
- * for the integrity attribute and its subtypes
- */
- for (slapi_entry_first_attr(search_entries[j], &attr); attr;
- slapi_entry_next_attr(search_entries[j], attr, &attr))
- {
- /*
- * Take into account only the subtypes of the attribute
- * in argv[i] having the necessary value - origDN
- */
- slapi_attr_get_type(attr, &attrName);
- if (slapi_attr_type_cmp(membership_attrs[i], attrName,
- SLAPI_TYPE_CMP_SUBTYPE) == 0)
- {
- nval = 0;
- slapi_attr_get_numvalues(attr, &nval);
- /*
- * We want to reduce the "modify" call as much as
- * possible. But if an entry contains 1000s of
- * attributes which need to be updated by the
- * referint plugin (e.g., a group containing 1000s
- * of members), we want to avoid to allocate too
- * many mods * in one "modify" call.
- * This is a compromise: If an attribute type has
- * more than 128 values, we update the attribute
- * value one by one. Otherwise, update all values
- * in one "modify" call.
- */
- if (nval > 128) {
- rc = _update_one_per_mod(
- slapi_entry_get_sdn(search_entries[j]),
- attr, attrName, origSDN, newrDN,
- slapi_sdn_get_dn(newsuperior),
- mod_pb);
- } else {
- rc = _update_all_per_mod(
- slapi_entry_get_sdn(search_entries[j]),
- attr, attrName, origSDN, newrDN,
- slapi_sdn_get_dn(newsuperior),
- mod_pb);
- }
- if (rc) {
- if (use_txn) {
- /*
- * We're using backend transactions,
- * so we need to stop on failure.
- */
- rc = SLAPI_PLUGIN_FAILURE;
- goto free_and_return;
- } else {
- rc = SLAPI_PLUGIN_SUCCESS;
- }
- }
- }
- }
- }
- } else {
- if (isFatalSearchError(search_result)){
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
- "update_integrity - Search (base=%s filter=%s) returned "
- "error %d\n", search_base, filter, search_result);
- rc = SLAPI_PLUGIN_FAILURE;
- goto free_and_return;
- }
- }
- slapi_ch_free_string(&filter);
- }
- slapi_free_search_results_internal(search_result_pb);
- }
- if (plugin_ContainerScope) {
- /* at the moment only a single scope is supported
- * so the loop ends after the first iteration
- */
- sdn = NULL;
- } else {
- sdn = slapi_get_next_suffix( &node, 0 );
- }
- }
- free_and_return:
- /* free filter and search_results_pb */
- slapi_ch_free_string(&filter);
- slapi_ch_array_free(membership_attrs);
- slapi_pblock_destroy(mod_pb);
- if (search_result_pb) {
- slapi_free_search_results_internal(search_result_pb);
- slapi_pblock_destroy(search_result_pb);
- }
-
- return(rc);
- }
- int referint_postop_start( Slapi_PBlock *pb)
- {
- Slapi_Entry *plugin_entry = NULL;
- Slapi_Entry *config_e = NULL;
- Slapi_PBlock *search_pb = NULL;
- Slapi_Entry *e = NULL;
- Slapi_DN *config_sdn = NULL;
- char *config_area = NULL;
- int result = 0;
- int rc = 0;
- if((config_rwlock = slapi_new_rwlock()) == NULL){
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM, "referint_postop_start - Failed to "
- "create rwlock.\n" );
- return ( -1 );
- }
- slapi_pblock_get( pb, SLAPI_ADD_ENTRY, &plugin_entry );
- /* Set the alternate config area if one is defined. */
- slapi_pblock_get(pb, SLAPI_PLUGIN_CONFIG_AREA, &config_area);
- if (config_area)
- {
- rc = slapi_dn_syntax_check(pb, config_area, 1);
- if (rc) { /* syntax check failed */
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM, "referint_postop_start - "
- "%s does not contain a valid DN (%s)\n",
- SLAPI_PLUGIN_SHARED_CONFIG_AREA, config_area);
- rc = LDAP_INVALID_DN_SYNTAX;
- goto bail;
- }
- config_sdn = slapi_sdn_new_dn_byval(config_area);
- result = slapi_search_internal_get_entry(config_sdn, NULL, &e, referint_plugin_identity);
- if (LDAP_SUCCESS != result) {
- if (result == LDAP_NO_SUCH_OBJECT) {
- /* log an error and use the plugin entry for the config */
- slapi_log_error(SLAPI_LOG_PLUGIN, REFERINT_PLUGIN_SUBSYSTEM,
- "referint_postop_start -Config entry \"%s\" does "
- "not exist.\n", config_area);
- rc = -1;
- goto bail;
- }
- } else {
- if(e){
- config_e = e;
- } else {
- slapi_log_error(SLAPI_LOG_PLUGIN, REFERINT_PLUGIN_SUBSYSTEM,
- "referint_postop_start - Config entry \"%s\" was not located.\n", config_area);
- rc = -1;
- goto bail;
- }
- }
- } else {
- config_e = plugin_entry;
- }
- if(load_config(pb, config_e, STARTUP) != LDAP_SUCCESS){
- rc = -1;
- goto bail;
- }
- referint_set_config_area(slapi_entry_get_sdn(config_e));
- /*
- * Only bother to start the thread if you are in delay mode.
- * 0 = no delay,
- * -1 = integrity off
- */
- if(referint_get_delay() > 0){
- /* initialize the cv and lock */
- if (!use_txn && (NULL == referint_mutex)) {
- referint_mutex = PR_NewLock();
- }
- keeprunning_mutex = PR_NewLock();
- keeprunning_cv = PR_NewCondVar(keeprunning_mutex);
- keeprunning =1;
- referint_tid = PR_CreateThread (PR_USER_THREAD,
- referint_thread_func,
- NULL,
- PR_PRIORITY_NORMAL,
- PR_GLOBAL_THREAD,
- PR_UNJOINABLE_THREAD,
- SLAPD_DEFAULT_THREAD_STACKSIZE);
- if ( referint_tid == NULL ) {
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
- "referint_postop_start - PR_CreateThread failed\n" );
- exit( 1 );
- }
- }
- bail:
- slapi_free_search_results_internal(search_pb);
- slapi_pblock_destroy(search_pb);
- slapi_sdn_free(&config_sdn);
- slapi_entry_free(e);
- return rc;
- }
- int referint_postop_close( Slapi_PBlock *pb)
- {
- /* signal the thread to exit */
- if (NULL != keeprunning_mutex) {
- PR_Lock(keeprunning_mutex);
- keeprunning=0;
- if (NULL != keeprunning_cv) {
- PR_NotifyCondVar(keeprunning_cv);
- }
- PR_Unlock(keeprunning_mutex);
- }
- slapi_destroy_rwlock(config_rwlock);
- config_rwlock = NULL;
- slapi_ch_free_string(&config->logfile);
- slapi_ch_array_free(config->attrs);
- slapi_ch_free((void **)&config);
- return(0);
- }
- void
- referint_thread_func(void *arg)
- {
- PRFileDesc *prfd = NULL;
- char *logfilename = NULL;
- char thisline[MAX_LINE];
- char delimiter[]="\t\n";
- char *ptoken;
- char *tmprdn;
- char *iter = NULL;
- Slapi_DN *sdn = NULL;
- Slapi_DN *tmpsuperior = NULL;
- int logChanges = 0;
- int delay;
- int no_changes;
- /*
- * keep running this thread until plugin is signaled to close
- */
- while(1){
- /* refresh the config */
- slapi_ch_free_string(&logfilename);
- referint_get_config(&delay, &logChanges, &logfilename);
- no_changes=1;
- while(no_changes){
- PR_Lock(keeprunning_mutex);
- if(keeprunning == 0){
- PR_Unlock(keeprunning_mutex);
- break;
- }
- PR_Unlock(keeprunning_mutex);
- referint_lock();
- if (( prfd = PR_Open( logfilename, PR_RDONLY, REFERINT_DEFAULT_FILE_MODE )) == NULL ){
- referint_unlock();
- /* go back to sleep and wait for this file */
- PR_Lock(keeprunning_mutex);
- PR_WaitCondVar(keeprunning_cv, PR_SecondsToInterval(delay));
- PR_Unlock(keeprunning_mutex);
- } else {
- no_changes = 0;
- }
- }
- /*
- * Check keep running here, because after break out of no
- * changes loop on shutdown, also need to break out of this
- * loop before trying to do the changes. The server
- * will pick them up on next startup as file still exists
- */
- PR_Lock(keeprunning_mutex);
- if(keeprunning == 0){
- PR_Unlock(keeprunning_mutex);
- break;
- }
- PR_Unlock(keeprunning_mutex);
- while( GetNextLine(thisline, MAX_LINE, prfd) ){
- ptoken = ldap_utf8strtok_r(thisline, delimiter, &iter);
- sdn = slapi_sdn_new_normdn_byref(ptoken);
- ptoken = ldap_utf8strtok_r (NULL, delimiter, &iter);
- if(!strcasecmp(ptoken, "NULL")) {
- tmprdn = NULL;
- } else {
- tmprdn = slapi_ch_smprintf("%s", ptoken);
- }
- ptoken = ldap_utf8strtok_r (NULL, delimiter, &iter);
- if (!strcasecmp(ptoken, "NULL")) {
- tmpsuperior = NULL;
- } else {
- tmpsuperior = slapi_sdn_new_normdn_byref(ptoken);
- }
- ptoken = ldap_utf8strtok_r (NULL, delimiter, &iter);
- if (strcasecmp(ptoken, "NULL") != 0) {
- /* Set the bind DN in the thread data */
- if(slapi_td_set_dn(slapi_ch_strdup(ptoken))){
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,"referint_thread_func - "
- "Failed to set thread data\n");
- }
- }
- update_integrity(sdn, tmprdn, tmpsuperior, logChanges);
- slapi_sdn_free(&sdn);
- slapi_ch_free_string(&tmprdn);
- slapi_sdn_free(&tmpsuperior);
- }
- PR_Close(prfd);
- /* remove the original file */
- if( PR_SUCCESS != PR_Delete(logfilename) ){
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
- "referint_thread_func - Could not delete \"%s\"\n", logfilename );
- }
- /* unlock and let other writers back at the file */
- referint_unlock();
- /* wait on condition here */
- PR_Lock(keeprunning_mutex);
- PR_WaitCondVar(keeprunning_cv, PR_SecondsToInterval(delay));
- PR_Unlock(keeprunning_mutex);
- }
- /* cleanup resources allocated in start */
- if (NULL != keeprunning_mutex) {
- PR_DestroyLock(keeprunning_mutex);
- }
- if (NULL != referint_mutex) {
- PR_DestroyLock(referint_mutex);
- }
- if (NULL != keeprunning_cv) {
- PR_DestroyCondVar(keeprunning_cv);
- }
- slapi_ch_free_string(&logfilename);
- }
- int my_fgetc(PRFileDesc *stream)
- {
- static char buf[READ_BUFSIZE] = "\0";
- static int position = READ_BUFSIZE;
- int retval;
- int err;
- /* check if we need to load the buffer */
- if( READ_BUFSIZE == position )
- {
- memset(buf, '\0', READ_BUFSIZE);
- if( ( err = PR_Read(stream, buf, READ_BUFSIZE) ) >= 0)
- {
- /* it read some data */;
- position = 0;
- }else{
- /* an error occurred */
- return err;
- }
- }
- /* try to read some data */
- if( '\0' == buf[position])
- {
- /* out of data, return eof */
- retval = MY_EOF;
- position = READ_BUFSIZE;
- }else{
- retval = buf[position];
- position++;
- }
- return retval;
- }
- int
- GetNextLine(char *dest, int size_dest, PRFileDesc *stream) {
- char nextchar ='\0';
- int done = 0;
- int i = 0;
- while(!done){
- if( ( nextchar = my_fgetc(stream) ) != 0){
- if( i < (size_dest - 1) ){
- dest[i] = nextchar;
- i++;
- if(nextchar == '\n'){
- /* end of line reached */
- done = 1;
- }
- } else {
- /* no more room in buffer */
- done = 1;
- }
- } else {
- /* error or end of file */
- done = 1;
- }
- }
- dest[i] = '\0';
-
- /* return size of string read */
- return i;
- }
- /*
- * Write this record to the log file
- */
- void
- writeintegritylog(Slapi_PBlock *pb, char *logfilename, Slapi_DN *sdn,
- char *newrdn, Slapi_DN *newsuperior, Slapi_DN *requestorsdn)
- {
- PRFileDesc *prfd;
- char buffer[MAX_LINE];
- int len_to_write = 0;
- int rc;
- const char *requestordn = NULL;
- const char *newsuperiordn = NULL;
- size_t reqdn_len = 0;
-
- if (!(referint_sdn_in_entry_scope(sdn) ||
- (newsuperior && referint_sdn_in_entry_scope(newsuperior)))) {
- return;
- }
- /*
- * Use this lock to protect file data when update integrity is occuring.
- * If betxn is enabled, this mutex is ignored; transaction itself takes
- * the role.
- */
- referint_lock();
- if (( prfd = PR_Open( logfilename, PR_WRONLY | PR_CREATE_FILE | PR_APPEND,
- REFERINT_DEFAULT_FILE_MODE )) == NULL )
- {
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
- "writeintegritylog - Could not write integrity log \"%s\" "
- SLAPI_COMPONENT_NAME_NSPR " %d (%s)\n",
- logfilename, PR_GetError(), slapd_pr_strerror(PR_GetError()) );
- PR_Unlock(referint_mutex);
- referint_unlock();
- return;
- }
- /*
- * Make sure we have enough room in our buffer before trying to write it.
- * add length of dn + 5(three tabs, a newline, and terminating \0)
- */
- len_to_write = slapi_sdn_get_ndn_len(sdn) + 5;
- newsuperiordn = slapi_sdn_get_dn(newsuperior);
- if (newsuperiordn &&
- !referint_sdn_in_entry_scope(newsuperior)) {
- /* this is a modrdn which moves the entry out of scope, handle like a delete */
- newsuperiordn = NULL;
- newrdn = NULL;
- }
- if(newrdn == NULL){
- /* add the length of "NULL" */
- len_to_write += 4;
- } else {
- /* add the length of the newrdn */
- len_to_write += strlen(newrdn);
- }
- if(NULL == newsuperiordn)
- {
- /* add the length of "NULL" */
- len_to_write += 4;
- } else {
- /* add the length of the newsuperior */
- len_to_write += slapi_sdn_get_ndn_len(newsuperior);
- }
- slapi_pblock_get(pb, SLAPI_REQUESTOR_DN, &requestordn);
- if (requestorsdn && (requestordn = slapi_sdn_get_udn(requestorsdn)) &&
- (reqdn_len = strlen(requestordn))) {
- len_to_write += reqdn_len;
- } else {
- len_to_write += 4; /* "NULL" */
- }
- if(len_to_write > MAX_LINE ){
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
- "writeintegritylog - Could not write integrity log:"
- " line length exceeded. It will not be able"
- " to update references to this entry.\n");
- } else {
- PR_snprintf(buffer, MAX_LINE, "%s\t%s\t%s\t%s\t\n", slapi_sdn_get_dn(sdn),
- (newrdn != NULL) ? newrdn : "NULL",
- (newsuperiordn != NULL) ? newsuperiordn : "NULL",
- requestordn ? requestordn : "NULL");
- if (PR_Write(prfd,buffer,strlen(buffer)) < 0){
- slapi_log_error(SLAPI_LOG_ERR,REFERINT_PLUGIN_SUBSYSTEM,
- " writeintegritylog - PR_Write failed : The disk"
- " may be full or the file is unwritable :: NSPR error - %d\n",
- PR_GetError());
- }
- }
- /* If file descriptor is closed successfully, PR_SUCCESS */
- rc = PR_Close(prfd);
- if (rc != PR_SUCCESS){
- slapi_log_error(SLAPI_LOG_ERR,REFERINT_PLUGIN_SUBSYSTEM,
- " writeintegritylog - Failed to close the file descriptor prfd; NSPR error - %d\n",
- PR_GetError());
- }
- referint_unlock();
- }
- static int
- referint_preop_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, premodfn, (void *)referint_validate_config) != 0)
- {
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
- "referint_preop_init - Failed to register plugin\n");
- status = -1;
- }
- return status;
- }
- /*
- * This is our preop function to validate a config update, postop modify
- * will apply the config change.
- */
- int
- referint_validate_config(Slapi_PBlock *pb)
- {
- Slapi_Entry *config_e = NULL, *e = NULL;
- Slapi_Entry *pre_entry = NULL;
- Slapi_Entry *resulting_entry = NULL;
- Slapi_DN *config_sdn = NULL;
- Slapi_DN *sdn = NULL;
- Slapi_Mods *smods = NULL;
- LDAPMod **mods = NULL;
- char *config_area = NULL;
- int rc = SLAPI_PLUGIN_SUCCESS;
- slapi_pblock_get(pb, SLAPI_TARGET_SDN, &sdn);
- slapi_pblock_get(pb, SLAPI_ENTRY_PRE_OP, &pre_entry);
- if (!pre_entry) {
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM, "referint_validate_config - Null pre op entry.\n");
- rc = LDAP_OPERATIONS_ERROR;
- goto bail;
- }
- if (referint_sdn_config_cmp(sdn) == 0 && slapi_sdn_compare(sdn, referint_get_plugin_area()) ){
- /*
- * This is the shared config entry. Apply the mods and set/validate
- * the config
- */
- slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
- smods = slapi_mods_new();
- slapi_mods_init_byref(smods, mods);
- /*
- * Create a copy of the entry and apply the
- * mods to create the resulting entry.
- */
- resulting_entry = slapi_entry_dup(pre_entry);
- if (mods && (slapi_entry_apply_mods(resulting_entry, mods) != LDAP_SUCCESS)) {
- /* we don't care about this, the update is invalid and will be caught later */
- goto bail;
- }
- if ( SLAPI_PLUGIN_FAILURE == load_config(pb, resulting_entry, 0)) {
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM, "referint_validate_config - "
- "configuration validation failed.\n");
- rc = LDAP_UNWILLING_TO_PERFORM;
- goto bail;
- }
- } else if (slapi_sdn_compare(sdn, referint_get_plugin_area()) == 0){
- /*
- * Check if the plugin config area is set(verify it and load its config),
- * otherwise reload the plugin entry config
- */
- slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
- smods = slapi_mods_new();
- slapi_mods_init_byref(smods, mods);
- /* Apply the mods to create the resulting entry. */
- resulting_entry = slapi_entry_dup(pre_entry);
- if (mods && (slapi_entry_apply_mods(resulting_entry, mods) != LDAP_SUCCESS)) {
- /* we don't care about this, the update is invalid and will be caught later */
- goto bail;
- }
- if((config_area = slapi_entry_attr_get_charptr(resulting_entry, SLAPI_PLUGIN_SHARED_CONFIG_AREA))){
- rc = slapi_dn_syntax_check(pb, config_area, 1);
- if (rc) { /* syntax check failed */
- slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM, "referint_validate_config - "
- "%s does not contain a valid DN (%s)\n",
- SLAPI_PLUGIN_SHARED_CONFIG_AREA, config_area);
- rc = LDAP_INVALID_DN_SYNTAX;
- goto bail;
- }
- config_sdn = slapi_sdn_new_dn_byval(config_area);
- rc = slapi_search_internal_get_entry(config_sdn, NULL, &e, referint_plugin_identity);
- if (LDAP_SUCCESS != rc) {
- /* log an error and use the plugin entry for the config */
- slapi_log_error(SLAPI_LOG_PLUGIN, REFERINT_PLUGIN_SUBSYSTEM,
- "referint_validate_config - Config entry \"%s\" couild not be found, error %d\n",
- config_area, rc);
- rc = LDAP_OPERATIONS_ERROR;
- goto bail;
- } else {
- if(e){
- config_e = e;
- } else {
- slapi_log_error(SLAPI_LOG_PLUGIN, REFERINT_PLUGIN_SUBSYSTEM,
- "referint_validate_config - Config entry \"%s\" was not located.\n", config_area);
- rc = LDAP_OPERATIONS_ERROR;
- goto bail;
- }
- }
- } else {
- config_e = resulting_entry;
- }
- if(load_config(pb, config_e, 0) != LDAP_SUCCESS){
- rc = LDAP_UNWILLING_TO_PERFORM;
- goto bail;
- }
- referint_set_config_area(slapi_entry_get_sdn(config_e));
- }
- bail:
- slapi_entry_free(e);
- slapi_entry_free(resulting_entry);
- slapi_sdn_free(&config_sdn);
- slapi_ch_free_string(&config_area);
- slapi_mods_free(&smods);
- return rc;
- }
|