1
0

uid.c 45 KB

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