uid.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /*
  42. * uid.c
  43. *
  44. * Implements a directory server pre-operation plugin to test
  45. * attributes for uniqueness within a defined subtree in the
  46. * directory.
  47. *
  48. * Called uid.c since the original purpose of the plugin was to
  49. * check the uid attribute in user entries.
  50. */
  51. #include <slapi-plugin.h>
  52. #include <portable.h>
  53. #include <string.h>
  54. #include "plugin-utils.h"
  55. #include "nspr.h"
  56. #if defined( LDAP_DEBUG ) && !defined( DEBUG )
  57. #define DEBUG
  58. #endif
  59. #define UNTAGGED_PARAMETER 12
  60. /* Quoting routine - this should be in a library somewhere (slapi?) */
  61. int ldap_quote_filter_value(
  62. char *value, int len,
  63. char *out, int maxLen,
  64. int *outLen);
  65. static int search_one_berval(Slapi_DN *baseDN, const char *attrName,
  66. const struct berval *value, const char *requiredObjectClass, Slapi_DN *target);
  67. /*
  68. * ISSUES:
  69. * How should this plugin handle ACL issues? It seems wrong to reject
  70. * adds and modifies because there is already a conflicting UID, when
  71. * the request would have failed because of an ACL check anyway.
  72. *
  73. * This code currently defines a maximum filter string size of 512. Is
  74. * this large enough?
  75. *
  76. * This code currently does not quote the value portion of the filter as
  77. * it is created. This is a bug.
  78. */
  79. /* */
  80. #define BEGIN do {
  81. #define END } while(0);
  82. /*
  83. * Slapi plugin descriptor
  84. */
  85. static char *plugin_name = "NSUniqueAttr";
  86. static Slapi_PluginDesc
  87. pluginDesc = {
  88. "NSUniqueAttr", VENDOR, DS_PACKAGE_VERSION,
  89. "Enforce unique attribute values"
  90. };
  91. static void* plugin_identity = NULL;
  92. typedef struct attr_uniqueness_config {
  93. char *attr;
  94. Slapi_DN **subtrees;
  95. PRBool unique_in_all_subtrees;
  96. char *top_entry_oc;
  97. char *subtree_entries_oc;
  98. struct attr_uniqueness_config *next;
  99. } attr_uniqueness_config_t;
  100. #define ATTR_UNIQUENESS_ATTRIBUTE_NAME "uniqueness-attribute-name"
  101. #define ATTR_UNIQUENESS_SUBTREES "uniqueness-subtrees"
  102. #define ATTR_UNIQUENESS_ACROSS_ALL_SUBTREES "uniqueness-across-all-subtrees"
  103. #define ATTR_UNIQUENESS_TOP_ENTRY_OC "uniqueness-top-entry-oc"
  104. #define ATTR_UNIQUENESS_SUBTREE_ENTRIES_OC "uniqueness-subtree-entries-oc"
  105. static int getArguments(Slapi_PBlock *pb, char **attrName, char **markerObjectClass, char **requiredObjectClass);
  106. static struct attr_uniqueness_config *uniqueness_entry_to_config(Slapi_PBlock *pb, Slapi_Entry *config_entry);
  107. /*
  108. * More information about constraint failure
  109. */
  110. static char *moreInfo =
  111. "Another entry with the same attribute value already exists (attribute: \"%s\")";
  112. static void
  113. free_uniqueness_config(struct attr_uniqueness_config *config)
  114. {
  115. int i;
  116. slapi_ch_free_string((char **) &config->attr);
  117. for (i = 0; config->subtrees && config->subtrees[i]; i++) {
  118. slapi_sdn_free(&config->subtrees[i]);
  119. }
  120. slapi_ch_free((void **) &config->subtrees);
  121. slapi_ch_free_string((char **) &config->top_entry_oc);
  122. slapi_ch_free_string((char **) &config->subtree_entries_oc);
  123. }
  124. /*
  125. * New styles:
  126. * ----------
  127. *
  128. * uniqueness-attribute-name: uid
  129. * uniqueness-subtrees: dc=people,dc=example,dc=com
  130. * uniqueness-subtrees: dc=sales, dc=example,dc=com
  131. * uniqueness-across-all-subtrees: on
  132. *
  133. * or
  134. *
  135. * uniqueness-attribute-name: uid
  136. * uniqueness-top-entry-oc: organizationalUnit
  137. * uniqueness-subtree-entries-oc: person
  138. *
  139. * If both are present:
  140. * - uniqueness-subtrees
  141. * - uniqueness-top-entry-oc/uniqueness-subtree-entries-oc
  142. * Then uniqueness-subtrees has the priority
  143. *
  144. * Old styles:
  145. * ----------
  146. *
  147. * nsslapd-pluginarg0: uid
  148. * nsslapd-pluginarg1: dc=people,dc=example,dc=com
  149. * nsslapd-pluginarg2: dc=sales, dc=example,dc=com
  150. *
  151. * or
  152. *
  153. * nsslapd-pluginarg0: attribute=uid
  154. * nsslapd-pluginarg1: markerobjectclass=organizationalUnit
  155. * nsslapd-pluginarg2: requiredobjectclass=person
  156. *
  157. * From a Slapi_Entry of the config entry, it creates a attr_uniqueness_config.
  158. * It returns a (attr_uniqueness_config *) if the configuration is valid
  159. * Else it returns NULL
  160. */
  161. static struct attr_uniqueness_config *
  162. uniqueness_entry_to_config(Slapi_PBlock *pb, Slapi_Entry *config_entry)
  163. {
  164. attr_uniqueness_config_t *tmp_config = NULL;
  165. char **values = NULL;
  166. int argc;
  167. char **argv = NULL;
  168. int rc = SLAPI_PLUGIN_SUCCESS;
  169. int i;
  170. int nb_subtrees = 0;
  171. if (config_entry == NULL) {
  172. rc = SLAPI_PLUGIN_FAILURE;
  173. goto done;
  174. }
  175. /* We are going to fill tmp_config in a first phase */
  176. if ((tmp_config = (attr_uniqueness_config_t *) slapi_ch_calloc(1, sizeof (attr_uniqueness_config_t))) == NULL) {
  177. slapi_log_error(SLAPI_LOG_FATAL, plugin_name, "load_config failed to allocate configuration\n");
  178. rc = SLAPI_PLUGIN_FAILURE;
  179. goto done;
  180. } else {
  181. /* set these to -1 for config validation */
  182. }
  183. /* Check if this is new/old config style */
  184. slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc);
  185. if (argc == 0) {
  186. /* This is new config style
  187. * uniqueness-attribute-name: uid
  188. * uniqueness-subtrees: dc=people,dc=example,dc=com
  189. * uniqueness-subtrees: dc=sales, dc=example,dc=com
  190. * uniqueness-across-all-subtrees: on
  191. *
  192. * or
  193. *
  194. * uniqueness-attribute-name: uid
  195. * uniqueness-top-entry-oc: organizationalUnit
  196. * uniqueness-subtree-entries-oc: person
  197. */
  198. /* Attribute name of the attribute we are going to check value uniqueness */
  199. tmp_config->attr = slapi_entry_attr_get_charptr(config_entry, ATTR_UNIQUENESS_ATTRIBUTE_NAME);
  200. /* Subtrees where uniqueness is tested */
  201. values = slapi_entry_attr_get_charray(config_entry, ATTR_UNIQUENESS_SUBTREES);
  202. if (values) {
  203. for (i = 0; values && values[i]; i++);
  204. if ((tmp_config->subtrees = (Slapi_DN **) slapi_ch_calloc(i + 1, sizeof (Slapi_DN *))) == NULL) {
  205. slapi_log_error(SLAPI_LOG_FATAL, plugin_name, "Config info: Fail to allocate subtree array \n");
  206. rc = SLAPI_PLUGIN_FAILURE;
  207. goto done;
  208. }
  209. /* copy the valid subtree DN into the config */
  210. for (i = 0, nb_subtrees = 0; values && values[i]; i++) {
  211. if (slapi_dn_syntax_check(pb, values[i], 1)) { /* syntax check failed */
  212. slapi_log_error(SLAPI_LOG_FATAL, plugin_name, "Config info: Invalid DN (skipped): %s\n", values[i]);
  213. continue;
  214. }
  215. tmp_config->subtrees[nb_subtrees] = slapi_sdn_new_dn_byval(values[i]);
  216. nb_subtrees++;
  217. }
  218. slapi_ch_array_free(values);
  219. values = NULL;
  220. }
  221. /* Uniqueness may be enforced accross all the subtrees, by default it is not */
  222. tmp_config->unique_in_all_subtrees = slapi_entry_attr_get_bool(config_entry, ATTR_UNIQUENESS_ACROSS_ALL_SUBTREES);
  223. /* enforce uniqueness only if the modified entry has this objectclass */
  224. tmp_config->top_entry_oc = slapi_entry_attr_get_charptr(config_entry, ATTR_UNIQUENESS_TOP_ENTRY_OC);
  225. /* enforce uniqueness, in the modified entry subtree, only to entries having this objectclass */
  226. tmp_config->subtree_entries_oc = slapi_entry_attr_get_charptr(config_entry, ATTR_UNIQUENESS_SUBTREE_ENTRIES_OC);
  227. } else {
  228. int result;
  229. char *attrName = NULL;
  230. char *markerObjectClass = NULL;
  231. char *requiredObjectClass = NULL;
  232. /* using the old style of configuration */
  233. result = getArguments(pb, &attrName, &markerObjectClass, &requiredObjectClass);
  234. if (LDAP_OPERATIONS_ERROR == result) {
  235. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name, "Config fail: unable to parse old style\n");
  236. rc = SLAPI_PLUGIN_FAILURE;
  237. goto done;
  238. }
  239. if (UNTAGGED_PARAMETER == result) {
  240. /* This is
  241. * nsslapd-pluginarg0: uid
  242. * nsslapd-pluginarg1: dc=people,dc=example,dc=com
  243. * nsslapd-pluginarg2: dc=sales, dc=example,dc=com
  244. *
  245. * config attribute are in argc/argv
  246. *
  247. * attrName is set
  248. * markerObjectClass/requiredObjectClass are NOT set
  249. */
  250. if (slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc) || slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv)) {
  251. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name, "Config fail: Only attribute name is valid\n");
  252. rc = SLAPI_PLUGIN_FAILURE;
  253. goto done;
  254. }
  255. /* Store attrName in the config */
  256. tmp_config->attr = slapi_ch_strdup(attrName);
  257. argc--;
  258. argv++; /* First argument was attribute name and remaining are subtrees */
  259. /* Store the subtrees */
  260. nb_subtrees = 0;
  261. if ((tmp_config->subtrees = (Slapi_DN **) slapi_ch_calloc(argc + 1, sizeof (Slapi_DN *))) == NULL) {
  262. slapi_log_error(SLAPI_LOG_FATAL, plugin_name, "Config info: Fail to allocate subtree array\n");
  263. rc = SLAPI_PLUGIN_FAILURE;
  264. goto done;
  265. }
  266. for (; argc > 0; argc--, argv++) {
  267. if (slapi_dn_syntax_check(pb, *argv, 1)) { /* syntax check failed */
  268. slapi_log_error(SLAPI_LOG_FATAL, plugin_name, "Config info: Invalid DN (skipped): %s\n", *argv);
  269. continue;
  270. }
  271. tmp_config->subtrees[nb_subtrees] = slapi_sdn_new_dn_byval(*argv);
  272. nb_subtrees++;
  273. }
  274. /* this interface does not configure accross subtree uniqueness*/
  275. tmp_config->unique_in_all_subtrees = PR_FALSE;
  276. /* Not really usefull, but it clarifies the config */
  277. tmp_config->subtree_entries_oc = NULL;
  278. tmp_config->top_entry_oc = NULL;
  279. } else {
  280. /* This is
  281. * nsslapd-pluginarg0: attribute=uid
  282. * nsslapd-pluginarg1: markerobjectclass=organizationalUnit
  283. * nsslapd-pluginarg2: requiredobjectclass=person
  284. *
  285. * config attributes are in
  286. * - attrName
  287. * - markerObjectClass
  288. * - requiredObjectClass
  289. */
  290. /* Store attrName in the config */
  291. tmp_config->attr = slapi_ch_strdup(attrName);
  292. /* There is no subtrees */
  293. tmp_config->subtrees = NULL;
  294. /* this interface does not configure accross subtree uniqueness*/
  295. tmp_config->unique_in_all_subtrees = PR_FALSE;
  296. /* set the objectclasses retrieved by getArgument */
  297. tmp_config->subtree_entries_oc = slapi_ch_strdup(requiredObjectClass);
  298. tmp_config->top_entry_oc = slapi_ch_strdup(markerObjectClass);
  299. }
  300. }
  301. /* Time to check that the new configuration is valid */
  302. if (tmp_config->attr == NULL) {
  303. slapi_log_error( SLAPI_LOG_FATAL, plugin_name, "Config info: attribute name not defined \n");
  304. rc = SLAPI_PLUGIN_FAILURE;
  305. goto done;
  306. }
  307. if (tmp_config->subtrees == NULL) {
  308. /* Uniqueness is enforced on entries matching objectclass */
  309. if (tmp_config->subtree_entries_oc == NULL) {
  310. slapi_log_error( SLAPI_LOG_FATAL, plugin_name, "Config info: objectclass for subtree entries is not defined\n");
  311. rc = SLAPI_PLUGIN_FAILURE;
  312. goto done;
  313. }
  314. } else if (tmp_config->subtrees[0] == NULL) {
  315. /* Uniqueness is enforced on subtrees but none are defined */
  316. slapi_log_error(SLAPI_LOG_FATAL, plugin_name, "Config info: No valid subtree is defined \n");
  317. rc = SLAPI_PLUGIN_FAILURE;
  318. goto done;
  319. }
  320. done:
  321. if (rc != SLAPI_PLUGIN_SUCCESS) {
  322. if (tmp_config) {
  323. free_uniqueness_config(tmp_config);
  324. slapi_ch_free((void **) &tmp_config);
  325. }
  326. return NULL;
  327. } else {
  328. return tmp_config;
  329. }
  330. }
  331. static void
  332. freePblock( Slapi_PBlock *spb ) {
  333. if ( spb )
  334. {
  335. slapi_free_search_results_internal( spb );
  336. slapi_pblock_destroy( spb );
  337. }
  338. }
  339. /* ------------------------------------------------------------ */
  340. /*
  341. * op_error - Record (and report) an operational error.
  342. * name changed to uid_op_error so as not to conflict with the external function
  343. * of the same name thereby preventing compiler warnings.
  344. */
  345. static int
  346. uid_op_error(int internal_error)
  347. {
  348. slapi_log_error(
  349. SLAPI_LOG_PLUGIN,
  350. plugin_name,
  351. "Internal error: %d\n",
  352. internal_error);
  353. return LDAP_OPERATIONS_ERROR;
  354. }
  355. /* ------------------------------------------------------------ */
  356. /*
  357. * Create an LDAP search filter from the attribute
  358. * name and value supplied.
  359. */
  360. static char *
  361. create_filter(const char *attribute, const struct berval *value, const char *requiredObjectClass)
  362. {
  363. char *filter;
  364. char *fp;
  365. char *max;
  366. int attrLen;
  367. int valueLen;
  368. int classLen = 0;
  369. int filterLen;
  370. PR_ASSERT(attribute);
  371. /* Compute the length of the required buffer */
  372. attrLen = strlen(attribute);
  373. if (ldap_quote_filter_value(value->bv_val,
  374. value->bv_len, 0, 0, &valueLen))
  375. return 0;
  376. if (requiredObjectClass) {
  377. classLen = strlen(requiredObjectClass);
  378. /* "(&(objectClass=)())" == 19 */
  379. filterLen = attrLen + 1 + valueLen + classLen + 19 + 1;
  380. } else {
  381. filterLen = attrLen + 1 + valueLen + 1;
  382. }
  383. /* Allocate the buffer */
  384. filter = slapi_ch_malloc(filterLen);
  385. fp = filter;
  386. max = &filter[filterLen];
  387. /* Place AND expression and objectClass in filter */
  388. if (requiredObjectClass) {
  389. strcpy(fp, "(&(objectClass=");
  390. fp += 15;
  391. strcpy(fp, requiredObjectClass);
  392. fp += classLen;
  393. *fp++ = ')';
  394. *fp++ = '(';
  395. }
  396. /* Place attribute name in filter */
  397. strcpy(fp, attribute);
  398. fp += attrLen;
  399. /* Place comparison operator */
  400. *fp++ = '=';
  401. /* Place value in filter */
  402. if (ldap_quote_filter_value(value->bv_val, value->bv_len,
  403. fp, max-fp, &valueLen)) { slapi_ch_free((void**)&filter); return 0; }
  404. fp += valueLen;
  405. /* Close AND expression if a requiredObjectClass was set */
  406. if (requiredObjectClass) {
  407. *fp++ = ')';
  408. *fp++ = ')';
  409. }
  410. /* Terminate */
  411. *fp = 0;
  412. return filter;
  413. }
  414. /* ------------------------------------------------------------ */
  415. /*
  416. * search - search a subtree for entries with a named attribute matching
  417. * the list of values. An entry matching the 'target' DN is
  418. * not considered in the search.
  419. *
  420. * If 'attr' is NULL, the values are taken from 'values'.
  421. * If 'attr' is non-NULL, the values are taken from 'attr'.
  422. *
  423. * Return:
  424. * LDAP_SUCCESS - no matches, or the attribute matches the
  425. * target dn.
  426. * LDAP_CONSTRAINT_VIOLATION - an entry was found that already
  427. * contains the attribute value.
  428. * LDAP_OPERATIONS_ERROR - a server failure.
  429. */
  430. static int
  431. search(Slapi_DN *baseDN, const char *attrName, Slapi_Attr *attr,
  432. struct berval **values, const char *requiredObjectClass,
  433. Slapi_DN *target)
  434. {
  435. int result;
  436. #ifdef DEBUG
  437. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  438. "SEARCH baseDN=%s attr=%s target=%s\n", slapi_sdn_get_dn(baseDN), attrName,
  439. target?slapi_sdn_get_dn(target):"None");
  440. #endif
  441. result = LDAP_SUCCESS;
  442. /* If no values, can't possibly be a conflict */
  443. if ( (Slapi_Attr *)NULL == attr && (struct berval **)NULL == values )
  444. return result;
  445. /*
  446. * Perform the search for each value provided
  447. *
  448. * Another possibility would be to search for all the values at once.
  449. * However, this is more complex (for filter creation) and unique
  450. * attributes values are probably only changed one at a time anyway.
  451. */
  452. if ( (Slapi_Attr *)NULL != attr )
  453. {
  454. Slapi_Value *v = NULL;
  455. int vhint = -1;
  456. for ( vhint = slapi_attr_first_value( attr, &v );
  457. vhint != -1 && LDAP_SUCCESS == result;
  458. vhint = slapi_attr_next_value( attr, vhint, &v ))
  459. {
  460. result = search_one_berval(baseDN, attrName,
  461. slapi_value_get_berval(v),
  462. requiredObjectClass, target);
  463. }
  464. }
  465. else
  466. {
  467. for (;*values != NULL && LDAP_SUCCESS == result; values++)
  468. {
  469. result = search_one_berval(baseDN, attrName, *values, requiredObjectClass,
  470. target);
  471. }
  472. }
  473. #ifdef DEBUG
  474. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  475. "SEARCH result = %d\n", result);
  476. #endif
  477. return( result );
  478. }
  479. static int
  480. search_one_berval(Slapi_DN *baseDN, const char *attrName,
  481. const struct berval *value, const char *requiredObjectClass,
  482. Slapi_DN *target)
  483. {
  484. int result;
  485. char *filter;
  486. Slapi_PBlock *spb;
  487. result = LDAP_SUCCESS;
  488. /* If no value, can't possibly be a conflict */
  489. if ( (struct berval *)NULL == value )
  490. return result;
  491. filter = 0;
  492. spb = 0;
  493. BEGIN
  494. int err;
  495. int sres;
  496. Slapi_Entry **entries;
  497. static char *attrs[] = { "1.1", 0 };
  498. /* Create the filter - this needs to be freed */
  499. filter = create_filter(attrName, value, requiredObjectClass);
  500. #ifdef DEBUG
  501. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  502. "SEARCH filter=%s\n", filter);
  503. #endif
  504. /* Perform the search using the new internal API */
  505. spb = slapi_pblock_new();
  506. if (!spb) { result = uid_op_error(2); break; }
  507. slapi_search_internal_set_pb_ext(spb, baseDN, LDAP_SCOPE_SUBTREE,
  508. filter, attrs, 0 /* attrs only */, NULL, NULL, plugin_identity, 0 /* actions */);
  509. slapi_search_internal_pb(spb);
  510. err = slapi_pblock_get(spb, SLAPI_PLUGIN_INTOP_RESULT, &sres);
  511. if (err) { result = uid_op_error(3); break; }
  512. /* Allow search to report that there is nothing in the subtree */
  513. if (sres == LDAP_NO_SUCH_OBJECT) break;
  514. /* Other errors are bad */
  515. if (sres) { result = uid_op_error(3); break; }
  516. err = slapi_pblock_get(spb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  517. &entries);
  518. if (err) { result = uid_op_error(4); break; }
  519. /*
  520. * Look at entries returned. Any entry found must be the
  521. * target entry or the constraint fails.
  522. */
  523. for(;*entries;entries++)
  524. {
  525. #ifdef DEBUG
  526. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  527. "SEARCH entry dn=%s\n", slapi_entry_get_dn(*entries));
  528. #endif
  529. /*
  530. * It is a Constraint Violation if any entry is found, unless
  531. * the entry is the target entry (if any).
  532. */
  533. if (!target || slapi_sdn_compare(slapi_entry_get_sdn(*entries), target) != 0)
  534. {
  535. result = LDAP_CONSTRAINT_VIOLATION;
  536. break;
  537. }
  538. }
  539. #ifdef DEBUG
  540. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  541. "SEARCH complete result=%d\n", result);
  542. #endif
  543. END
  544. /* Clean-up */
  545. if (spb) {
  546. slapi_free_search_results_internal(spb);
  547. slapi_pblock_destroy(spb);
  548. }
  549. slapi_ch_free((void**)&filter);
  550. return result;
  551. }
  552. /* ------------------------------------------------------------ */
  553. /*
  554. * searchAllSubtrees - search all subtrees in argv for entries
  555. * with a named attribute matching the list of values, by
  556. * calling search for each one.
  557. *
  558. * If 'attr' is NULL, the values are taken from 'values'.
  559. * If 'attr' is non-NULL, the values are taken from 'attr'.
  560. *
  561. * Return:
  562. * LDAP_SUCCESS - no matches, or the attribute matches the
  563. * target dn.
  564. * LDAP_CONSTRAINT_VIOLATION - an entry was found that already
  565. * contains the attribute value.
  566. * LDAP_OPERATIONS_ERROR - a server failure.
  567. */
  568. static int
  569. searchAllSubtrees(Slapi_DN **subtrees, const char *attrName,
  570. Slapi_Attr *attr, struct berval **values, const char *requiredObjectClass,
  571. Slapi_DN *dn, PRBool unique_in_all_subtrees)
  572. {
  573. int result = LDAP_SUCCESS;
  574. int i;
  575. if (unique_in_all_subtrees) {
  576. PRBool in_a_subtree = PR_FALSE;
  577. /* we need to check that the added values of this attribute
  578. * are unique in all the monitored subtrees
  579. */
  580. /* First check the target entry is in one of
  581. * the monitored subtree, so adding 'values' would
  582. * violate constraint
  583. */
  584. for (i = 0;subtrees && subtrees[i]; i++) {
  585. if (slapi_sdn_issuffix(dn, subtrees[i])) {
  586. in_a_subtree = PR_TRUE;
  587. break;
  588. }
  589. }
  590. if (! in_a_subtree) {
  591. return result;
  592. }
  593. }
  594. /*
  595. * For each DN in the managed list, do uniqueness checking if
  596. * the target DN is a subnode in the tree.
  597. */
  598. for(i = 0;subtrees && subtrees[i]; i++)
  599. {
  600. Slapi_DN *sufdn = subtrees[i];
  601. /*
  602. * The DN should already be normalized, so we don't have to
  603. * worry about that here.
  604. */
  605. if (unique_in_all_subtrees || slapi_sdn_issuffix(dn, sufdn)) {
  606. result = search(sufdn, attrName, attr, values, requiredObjectClass, dn);
  607. if (result) break;
  608. }
  609. }
  610. return result;
  611. }
  612. /* ------------------------------------------------------------ */
  613. /*
  614. * getArguments - parse invocation parameters
  615. * Return:
  616. * 0 - success
  617. * >0 - error parsing parameters
  618. */
  619. static int
  620. getArguments(Slapi_PBlock *pb, char **attrName, char **markerObjectClass,
  621. char **requiredObjectClass)
  622. {
  623. int argc;
  624. char **argv;
  625. /*
  626. * Get the arguments
  627. */
  628. if (slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc))
  629. {
  630. return uid_op_error(10);
  631. }
  632. if (slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv))
  633. {
  634. return uid_op_error(11);
  635. }
  636. /*
  637. * Required arguments: attribute and markerObjectClass
  638. * Optional argument: requiredObjectClass
  639. */
  640. for(;argc > 0;argc--,argv++)
  641. {
  642. char *param = *argv;
  643. char *delimiter = strchr(param, '=');
  644. if (NULL == delimiter)
  645. {
  646. /* Old style untagged parameter */
  647. *attrName = *argv;
  648. return UNTAGGED_PARAMETER;
  649. }
  650. if (strncasecmp(param, "attribute", delimiter-param) == 0)
  651. {
  652. /* It's OK to set a pointer here, because ultimately it points
  653. * inside the argv array of the pblock, which will be staying
  654. * arround.
  655. */
  656. *attrName = delimiter+1;
  657. } else if (strncasecmp(param, "markerobjectclass", delimiter-param) == 0)
  658. {
  659. *markerObjectClass = delimiter+1;
  660. } else if (strncasecmp(param, "requiredobjectclass", delimiter-param) == 0)
  661. {
  662. *requiredObjectClass = delimiter+1;
  663. }
  664. }
  665. if (!*attrName || !*markerObjectClass)
  666. {
  667. return uid_op_error(13);
  668. }
  669. return 0;
  670. }
  671. /* ------------------------------------------------------------ */
  672. /*
  673. * findSubtreeAndSearch - walk up the tree to find an entry with
  674. * the marker object class; if found, call search from there and
  675. * return the result it returns
  676. *
  677. * If 'attr' is NULL, the values are taken from 'values'.
  678. * If 'attr' is non-NULL, the values are taken from 'attr'.
  679. *
  680. * Return:
  681. * LDAP_SUCCESS - no matches, or the attribute matches the
  682. * target dn.
  683. * LDAP_CONSTRAINT_VIOLATION - an entry was found that already
  684. * contains the attribute value.
  685. * LDAP_OPERATIONS_ERROR - a server failure.
  686. */
  687. static int
  688. findSubtreeAndSearch(Slapi_DN *parentDN, const char *attrName, Slapi_Attr *attr,
  689. struct berval **values, const char *requiredObjectClass, Slapi_DN *target,
  690. const char *markerObjectClass)
  691. {
  692. int result = LDAP_SUCCESS;
  693. Slapi_PBlock *spb = NULL;
  694. Slapi_DN *curpar = slapi_sdn_new();
  695. Slapi_DN *newpar = NULL;
  696. slapi_sdn_get_parent(parentDN, curpar);
  697. while (slapi_sdn_get_dn(curpar) != NULL)
  698. {
  699. if ((spb = dnHasObjectClass(curpar, markerObjectClass)))
  700. {
  701. freePblock(spb);
  702. /*
  703. * Do the search. There is no entry that is allowed
  704. * to have the attribute already.
  705. */
  706. result = search(curpar, attrName, attr, values, requiredObjectClass,
  707. target);
  708. break;
  709. }
  710. newpar = slapi_sdn_new();
  711. slapi_sdn_copy(curpar, newpar);
  712. slapi_sdn_get_parent(newpar, curpar);
  713. slapi_sdn_free(&newpar);
  714. }
  715. slapi_sdn_free(&curpar);
  716. return result;
  717. }
  718. /* ------------------------------------------------------------ */
  719. /*
  720. * preop_add - pre-operation plug-in for add
  721. */
  722. static int
  723. preop_add(Slapi_PBlock *pb)
  724. {
  725. int result;
  726. char *errtext = NULL;
  727. char *attrName = NULL;
  728. #ifdef DEBUG
  729. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name, "ADD begin\n");
  730. #endif
  731. result = LDAP_SUCCESS;
  732. /*
  733. * Do constraint check on the added entry. Set result.
  734. */
  735. BEGIN
  736. int err;
  737. char *markerObjectClass = NULL;
  738. char *requiredObjectClass = NULL;
  739. Slapi_DN *sdn = NULL;
  740. int isupdatedn;
  741. Slapi_Entry *e;
  742. Slapi_Attr *attr;
  743. struct attr_uniqueness_config *config = NULL;
  744. /*
  745. * If this is a replication update, just be a noop.
  746. */
  747. err = slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &isupdatedn);
  748. if (err) { result = uid_op_error(50); break; }
  749. if (isupdatedn)
  750. {
  751. break;
  752. }
  753. slapi_pblock_get(pb, SLAPI_PLUGIN_PRIVATE, &config);
  754. if (config == NULL) {
  755. slapi_log_error(SLAPI_LOG_FATAL, plugin_name, "preop_modrdn fail to retrieve the config\n");
  756. result = LDAP_OPERATIONS_ERROR;
  757. break;
  758. }
  759. /*
  760. * Get the arguments
  761. */
  762. attrName = config->attr;
  763. markerObjectClass = config->top_entry_oc;
  764. requiredObjectClass = config->subtree_entries_oc;
  765. /*
  766. * Get the target DN for this add operation
  767. */
  768. err = slapi_pblock_get(pb, SLAPI_ADD_TARGET_SDN, &sdn);
  769. if (err) { result = uid_op_error(51); break; }
  770. #ifdef DEBUG
  771. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name, "ADD target=%s\n", slapi_sdn_get_dn(sdn));
  772. #endif
  773. /*
  774. * Get the entry data for this add. Check whether it
  775. * contains a value for the unique attribute
  776. */
  777. err = slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e);
  778. if (err) { result = uid_op_error(52); break; }
  779. err = slapi_entry_attr_find(e, attrName, &attr);
  780. if (err) break; /* no unique attribute */
  781. /*
  782. * Check if it contains the required object class
  783. */
  784. if (NULL != requiredObjectClass)
  785. {
  786. if (!entryHasObjectClass(pb, e, requiredObjectClass))
  787. {
  788. /* No, so we don't have to do anything */
  789. break;
  790. }
  791. }
  792. /*
  793. * Passed all the requirements - this is an operation we
  794. * need to enforce uniqueness on. Now find all parent entries
  795. * with the marker object class, and do a search for each one.
  796. */
  797. if (NULL != markerObjectClass)
  798. {
  799. /* Subtree defined by location of marker object class */
  800. result = findSubtreeAndSearch(sdn, attrName, attr, NULL,
  801. requiredObjectClass, sdn,
  802. markerObjectClass);
  803. } else
  804. {
  805. /* Subtrees listed on invocation line */
  806. result = searchAllSubtrees(config->subtrees, attrName, attr, NULL,
  807. requiredObjectClass, sdn, config->unique_in_all_subtrees);
  808. }
  809. END
  810. if (result)
  811. {
  812. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  813. "ADD result %d\n", result);
  814. if (result == LDAP_CONSTRAINT_VIOLATION) {
  815. errtext = slapi_ch_smprintf(moreInfo, attrName);
  816. } else {
  817. errtext = slapi_ch_strdup("Error checking for attribute uniqueness.");
  818. }
  819. /* Send failure to the client */
  820. slapi_send_ldap_result(pb, result, 0, errtext, 0, 0);
  821. slapi_ch_free_string(&errtext);
  822. }
  823. return (result==LDAP_SUCCESS)?0:-1;
  824. }
  825. static void
  826. addMod(LDAPMod ***modary, int *capacity, int *nmods, LDAPMod *toadd)
  827. {
  828. if (*nmods == *capacity) {
  829. *capacity += 4;
  830. if (*modary) {
  831. *modary = (LDAPMod **)slapi_ch_realloc((char *)*modary, *capacity * sizeof(LDAPMod *));
  832. } else {
  833. *modary = (LDAPMod **)slapi_ch_malloc(*capacity * sizeof(LDAPMod *));
  834. }
  835. }
  836. (*modary)[*nmods] = toadd;
  837. (*nmods)++;
  838. }
  839. /* ------------------------------------------------------------ */
  840. /*
  841. * preop_modify - pre-operation plug-in for modify
  842. */
  843. static int
  844. preop_modify(Slapi_PBlock *pb)
  845. {
  846. int result = LDAP_SUCCESS;
  847. Slapi_PBlock *spb = NULL;
  848. LDAPMod **checkmods = NULL;
  849. int checkmodsCapacity = 0;
  850. char *errtext = NULL;
  851. char *attrName = NULL;
  852. struct attr_uniqueness_config *config = NULL;
  853. #ifdef DEBUG
  854. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  855. "MODIFY begin\n");
  856. #endif
  857. BEGIN
  858. int err;
  859. char *markerObjectClass=NULL;
  860. char *requiredObjectClass=NULL;
  861. LDAPMod **mods;
  862. int modcount = 0;
  863. int ii;
  864. LDAPMod *mod;
  865. Slapi_DN *sdn = NULL;
  866. int isupdatedn;
  867. /*
  868. * If this is a replication update, just be a noop.
  869. */
  870. err = slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &isupdatedn);
  871. if (err) { result = uid_op_error(60); break; }
  872. if (isupdatedn)
  873. {
  874. break;
  875. }
  876. slapi_pblock_get(pb, SLAPI_PLUGIN_PRIVATE, &config);
  877. if (config == NULL) {
  878. slapi_log_error(SLAPI_LOG_FATAL, plugin_name, "preop_modrdn fail to retrieve the config\n");
  879. result = LDAP_OPERATIONS_ERROR;
  880. break;
  881. }
  882. /*
  883. * Get the arguments
  884. */
  885. attrName = config->attr;
  886. markerObjectClass = config->top_entry_oc;
  887. requiredObjectClass = config->subtree_entries_oc;
  888. err = slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  889. if (err) { result = uid_op_error(61); break; }
  890. /* There may be more than one mod that matches e.g.
  891. changetype: modify
  892. delete: uid
  893. uid: balster1950
  894. -
  895. add: uid
  896. uid: scottg
  897. So, we need to first find all mods that contain the attribute
  898. which are add or replace ops and are bvalue encoded
  899. */
  900. /* find out how many mods meet this criteria */
  901. for(;mods && *mods;mods++)
  902. {
  903. mod = *mods;
  904. if ((slapi_attr_type_cmp(mod->mod_type, attrName, 1) == 0) && /* mod contains target attr */
  905. (mod->mod_op & LDAP_MOD_BVALUES) && /* mod is bval encoded (not string val) */
  906. (mod->mod_bvalues && mod->mod_bvalues[0]) && /* mod actually contains some values */
  907. (SLAPI_IS_MOD_ADD(mod->mod_op) || /* mod is add */
  908. SLAPI_IS_MOD_REPLACE(mod->mod_op))) /* mod is replace */
  909. {
  910. addMod(&checkmods, &checkmodsCapacity, &modcount, mod);
  911. }
  912. }
  913. if (modcount == 0) {
  914. break; /* no mods to check, we are done */
  915. }
  916. /* Get the target DN */
  917. err = slapi_pblock_get(pb, SLAPI_MODIFY_TARGET_SDN, &sdn);
  918. if (err) { result = uid_op_error(11); break; }
  919. /*
  920. * Check if it has the required object class
  921. */
  922. if (requiredObjectClass &&
  923. !(spb = dnHasObjectClass(sdn, requiredObjectClass))) {
  924. break;
  925. }
  926. /*
  927. * Passed all the requirements - this is an operation we
  928. * need to enforce uniqueness on. Now find all parent entries
  929. * with the marker object class, and do a search for each one.
  930. */
  931. /*
  932. * stop checking at first mod that fails the check
  933. */
  934. for (ii = 0; (result == 0) && (ii < modcount); ++ii)
  935. {
  936. mod = checkmods[ii];
  937. if (NULL != markerObjectClass)
  938. {
  939. /* Subtree defined by location of marker object class */
  940. result = findSubtreeAndSearch(sdn, attrName, NULL,
  941. mod->mod_bvalues, requiredObjectClass,
  942. sdn, markerObjectClass);
  943. } else
  944. {
  945. /* Subtrees listed on invocation line */
  946. result = searchAllSubtrees(config->subtrees, attrName, NULL,
  947. mod->mod_bvalues, requiredObjectClass, sdn, config->unique_in_all_subtrees);
  948. }
  949. }
  950. END
  951. slapi_ch_free((void **)&checkmods);
  952. freePblock(spb);
  953. if (result)
  954. {
  955. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  956. "MODIFY result %d\n", result);
  957. if (result == LDAP_CONSTRAINT_VIOLATION) {
  958. errtext = slapi_ch_smprintf(moreInfo, attrName);
  959. } else {
  960. errtext = slapi_ch_strdup("Error checking for attribute uniqueness.");
  961. }
  962. slapi_send_ldap_result(pb, result, 0, errtext, 0, 0);
  963. slapi_ch_free_string(&errtext);
  964. }
  965. return (result==LDAP_SUCCESS)?0:-1;
  966. }
  967. /* ------------------------------------------------------------ */
  968. /*
  969. * preop_modrdn - Pre-operation call for modify RDN
  970. *
  971. * Check that the new RDN does not include attributes that
  972. * cause a constraint violation
  973. */
  974. static int
  975. preop_modrdn(Slapi_PBlock *pb)
  976. {
  977. int result = LDAP_SUCCESS;
  978. Slapi_Entry *e = NULL;
  979. Slapi_Value *sv_requiredObjectClass = NULL;
  980. char *errtext = NULL;
  981. char *attrName = NULL;
  982. struct attr_uniqueness_config *config = NULL;
  983. #ifdef DEBUG
  984. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  985. "MODRDN begin\n");
  986. #endif
  987. BEGIN
  988. int err;
  989. char *markerObjectClass=NULL;
  990. char *requiredObjectClass=NULL;
  991. Slapi_DN *sdn = NULL;
  992. Slapi_DN *superior;
  993. char *rdn;
  994. int deloldrdn = 0;
  995. int isupdatedn;
  996. Slapi_Attr *attr;
  997. /*
  998. * If this is a replication update, just be a noop.
  999. */
  1000. err = slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &isupdatedn);
  1001. if (err) { result = uid_op_error(30); break; }
  1002. if (isupdatedn)
  1003. {
  1004. break;
  1005. }
  1006. slapi_pblock_get(pb, SLAPI_PLUGIN_PRIVATE, &config);
  1007. if (config == NULL) {
  1008. slapi_log_error(SLAPI_LOG_FATAL, plugin_name, "preop_modrdn fail to retrieve the config\n");
  1009. result = LDAP_OPERATIONS_ERROR;
  1010. break;
  1011. }
  1012. /*
  1013. * Get the arguments
  1014. */
  1015. attrName = config->attr;
  1016. markerObjectClass = config->top_entry_oc;
  1017. requiredObjectClass = config->subtree_entries_oc;
  1018. /* Create a Slapi_Value for the requiredObjectClass to use
  1019. * for checking the entry. */
  1020. if (requiredObjectClass) {
  1021. sv_requiredObjectClass = slapi_value_new_string(requiredObjectClass);
  1022. }
  1023. /* Get the DN of the entry being renamed */
  1024. err = slapi_pblock_get(pb, SLAPI_MODRDN_TARGET_SDN, &sdn);
  1025. if (err) { result = uid_op_error(31); break; }
  1026. /* Get superior value - unimplemented in 3.0/4.0/5.0 DS */
  1027. err = slapi_pblock_get(pb, SLAPI_MODRDN_NEWSUPERIOR_SDN, &superior);
  1028. if (err) { result = uid_op_error(32); break; }
  1029. /*
  1030. * No superior means the entry is just renamed at
  1031. * its current level in the tree. Use the target DN for
  1032. * determining which managed tree this belongs to
  1033. */
  1034. if (!superior) superior = sdn;
  1035. /* Get the new RDN - this has the attribute values */
  1036. err = slapi_pblock_get(pb, SLAPI_MODRDN_NEWRDN, &rdn);
  1037. if (err) { result = uid_op_error(33); break; }
  1038. #ifdef DEBUG
  1039. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  1040. "MODRDN newrdn=%s\n", rdn);
  1041. #endif
  1042. /* See if the old RDN value is being deleted. */
  1043. err = slapi_pblock_get(pb, SLAPI_MODRDN_DELOLDRDN, &deloldrdn);
  1044. if (err) { result = uid_op_error(34); break; }
  1045. /* Get the entry that is being renamed so we can make a dummy copy
  1046. * of what it will look like after the rename. */
  1047. err = slapi_search_internal_get_entry(sdn, NULL, &e, plugin_identity);
  1048. if (err != LDAP_SUCCESS) {
  1049. result = uid_op_error(35);
  1050. /* We want to return a no such object error if the target doesn't exist. */
  1051. if (err == LDAP_NO_SUCH_OBJECT) {
  1052. result = err;
  1053. }
  1054. break;
  1055. }
  1056. /* Apply the rename operation to the dummy entry. */
  1057. /* slapi_entry_rename does not expect rdn normalized */
  1058. err = slapi_entry_rename(e, rdn, deloldrdn, superior);
  1059. if (err != LDAP_SUCCESS) { result = uid_op_error(36); break; }
  1060. /*
  1061. * Find any unique attribute data in the new RDN
  1062. */
  1063. err = slapi_entry_attr_find(e, attrName, &attr);
  1064. if (err) break; /* no UID attribute */
  1065. /*
  1066. * Check if it has the required object class
  1067. */
  1068. if (requiredObjectClass &&
  1069. !slapi_entry_attr_has_syntax_value(e, SLAPI_ATTR_OBJECTCLASS, sv_requiredObjectClass)) { break; }
  1070. /*
  1071. * Passed all the requirements - this is an operation we
  1072. * need to enforce uniqueness on. Now find all parent entries
  1073. * with the marker object class, and do a search for each one.
  1074. */
  1075. if (NULL != markerObjectClass)
  1076. {
  1077. /* Subtree defined by location of marker object class */
  1078. result = findSubtreeAndSearch(slapi_entry_get_sdn(e), attrName, attr, NULL,
  1079. requiredObjectClass, sdn,
  1080. markerObjectClass);
  1081. } else
  1082. {
  1083. /* Subtrees listed on invocation line */
  1084. result = searchAllSubtrees(config->subtrees, attrName, attr, NULL,
  1085. requiredObjectClass, sdn, config->unique_in_all_subtrees);
  1086. }
  1087. END
  1088. /* Clean-up */
  1089. slapi_value_free(&sv_requiredObjectClass);
  1090. if (e) slapi_entry_free(e);
  1091. if (result)
  1092. {
  1093. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  1094. "MODRDN result %d\n", result);
  1095. if (result == LDAP_CONSTRAINT_VIOLATION) {
  1096. errtext = slapi_ch_smprintf(moreInfo, attrName);
  1097. } else {
  1098. errtext = slapi_ch_strdup("Error checking for attribute uniqueness.");
  1099. }
  1100. slapi_send_ldap_result(pb, result, 0, errtext, 0, 0);
  1101. slapi_ch_free_string(&errtext);
  1102. }
  1103. return (result==LDAP_SUCCESS)?0:-1;
  1104. }
  1105. /* ------------------------------------------------------------ */
  1106. /*
  1107. * Initialize the plugin
  1108. *
  1109. * uidunique_init (the old name) is deprecated
  1110. */
  1111. int
  1112. NSUniqueAttr_Init(Slapi_PBlock *pb)
  1113. {
  1114. int err = 0;
  1115. Slapi_Entry *plugin_entry = NULL;
  1116. char *plugin_type = NULL;
  1117. int preadd = SLAPI_PLUGIN_PRE_ADD_FN;
  1118. int premod = SLAPI_PLUGIN_PRE_MODIFY_FN;
  1119. int premdn = SLAPI_PLUGIN_PRE_MODRDN_FN;
  1120. struct attr_uniqueness_config *config = NULL;
  1121. BEGIN
  1122. /* Declare plugin version */
  1123. err = slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  1124. SLAPI_PLUGIN_VERSION_01);
  1125. if (err) break;
  1126. /*
  1127. * Get plugin identity and store it for later use
  1128. * Used for internal operations
  1129. */
  1130. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &plugin_identity);
  1131. /* PR_ASSERT (plugin_identity); */
  1132. if ((slapi_pblock_get(pb, SLAPI_PLUGIN_CONFIG_ENTRY, &plugin_entry) == 0) &&
  1133. plugin_entry &&
  1134. (plugin_type = slapi_entry_attr_get_charptr(plugin_entry, "nsslapd-plugintype")) &&
  1135. plugin_type && strstr(plugin_type, "betxn")) {
  1136. preadd = SLAPI_PLUGIN_BE_TXN_PRE_ADD_FN;
  1137. premod = SLAPI_PLUGIN_BE_TXN_PRE_MODIFY_FN;
  1138. premdn = SLAPI_PLUGIN_BE_TXN_PRE_MODRDN_FN;
  1139. }
  1140. slapi_ch_free_string(&plugin_type);
  1141. /* load the config into the config list */
  1142. if ((config = uniqueness_entry_to_config(pb, plugin_entry)) == NULL) {
  1143. err = SLAPI_PLUGIN_FAILURE;
  1144. break;
  1145. }
  1146. slapi_pblock_set(pb, SLAPI_PLUGIN_PRIVATE, (void*) config);
  1147. /* Provide descriptive information */
  1148. err = slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  1149. (void*)&pluginDesc);
  1150. if (err) break;
  1151. /* Register functions */
  1152. err = slapi_pblock_set(pb, preadd, (void*)preop_add);
  1153. if (err) break;
  1154. err = slapi_pblock_set(pb, premod, (void*)preop_modify);
  1155. if (err) break;
  1156. err = slapi_pblock_set(pb, premdn, (void*)preop_modrdn);
  1157. if (err) break;
  1158. END
  1159. if (err) {
  1160. slapi_log_error(SLAPI_LOG_PLUGIN, "NSUniqueAttr_Init",
  1161. "Error: %d\n", err);
  1162. if (config) {
  1163. slapi_pblock_set(pb, SLAPI_PLUGIN_PRIVATE, NULL);
  1164. free_uniqueness_config(config);
  1165. slapi_ch_free((void **) &config);
  1166. }
  1167. err = -1;
  1168. }
  1169. else
  1170. slapi_log_error(SLAPI_LOG_PLUGIN, "NSUniqueAttr_Init",
  1171. "plugin loaded\n");
  1172. return err;
  1173. }
  1174. int
  1175. uidunique_init(Slapi_PBlock *pb)
  1176. {
  1177. return NSUniqueAttr_Init(pb);
  1178. }
  1179. /* ------------------------------------------------------------ */
  1180. /*
  1181. * ldap_quote_filter_value
  1182. *
  1183. * Quote the filter value according to RFC 2254 (Dec 1997)
  1184. *
  1185. * value - a UTF8 string containing the value. It may contain
  1186. * any of the chars needing quotes ( '(' ')' '*' '/' and NUL ).
  1187. * len - the length of the UTF8 value
  1188. * out - a buffer to recieve the converted value. May be NULL, in
  1189. * which case, only the length of the output is computed (and placed in
  1190. * outLen).
  1191. * maxLen - the size of the output buffer. It is an error if this length
  1192. * is exceeded. Ignored if out is NULL.
  1193. * outLen - recieves the size of the output. If an error occurs, this
  1194. * result is not available.
  1195. *
  1196. * Returns
  1197. * 0 - success
  1198. * -1 - failure (usually a buffer overflow)
  1199. */
  1200. int /* Error value */
  1201. ldap_quote_filter_value(
  1202. char *value, int len,
  1203. char *out, int maxLen,
  1204. int *outLen)
  1205. {
  1206. int err;
  1207. char *eValue;
  1208. int resLen;
  1209. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  1210. static char hexchars[16] = "0123456789abcdef";
  1211. #endif
  1212. err = 0;
  1213. eValue = &value[len];
  1214. resLen = 0;
  1215. /*
  1216. * Convert each character in the input string
  1217. */
  1218. while(value < eValue)
  1219. {
  1220. switch(*value)
  1221. {
  1222. case '(':
  1223. case ')':
  1224. case '*':
  1225. case '\\':
  1226. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  1227. case 0:
  1228. #endif
  1229. /* Handle characters needing special escape sequences */
  1230. /* Compute size of output */
  1231. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  1232. resLen += 3;
  1233. #else
  1234. resLen += 2;
  1235. #endif
  1236. /* Generate output if requested */
  1237. if (out)
  1238. {
  1239. /* Check for overflow */
  1240. if (resLen > maxLen) { err = -1; break; }
  1241. *out++ = '\\';
  1242. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  1243. *out++ = hexchars[(*value >> 4) & 0xF];
  1244. *out++ = hexchars[*value & 0xF];
  1245. #else
  1246. *out++ = *value;
  1247. #endif
  1248. }
  1249. break;
  1250. default:
  1251. /* Compute size of output */
  1252. resLen += 1;
  1253. /* Generate output if requested */
  1254. if (out)
  1255. {
  1256. if (resLen > maxLen) { err = -1; break; }
  1257. *out++ = *value;
  1258. }
  1259. break;
  1260. }
  1261. if (err) break;
  1262. value++;
  1263. }
  1264. if (!err) *outLen = resLen;
  1265. return err;
  1266. }