uid.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477
  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. static int
  1103. uiduniq_start(Slapi_PBlock *pb)
  1104. {
  1105. Slapi_Entry *plugin_entry = NULL;
  1106. struct attr_uniqueness_config *config = NULL;
  1107. if (slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &plugin_entry) == 0){
  1108. /* load the config into the config list */
  1109. if ((config = uniqueness_entry_to_config(pb, plugin_entry)) == NULL) {
  1110. return SLAPI_PLUGIN_FAILURE;
  1111. }
  1112. slapi_pblock_set(pb, SLAPI_PLUGIN_PRIVATE, (void*) config);
  1113. }
  1114. return 0;
  1115. }
  1116. static int
  1117. uiduniq_close(Slapi_PBlock *pb)
  1118. {
  1119. struct attr_uniqueness_config *config = NULL;
  1120. slapi_pblock_get(pb, SLAPI_PLUGIN_PRIVATE, &config);
  1121. if (config) {
  1122. slapi_pblock_set(pb, SLAPI_PLUGIN_PRIVATE, NULL);
  1123. free_uniqueness_config(config);
  1124. slapi_ch_free((void **) &config);
  1125. }
  1126. return 0;
  1127. }
  1128. /* ------------------------------------------------------------ */
  1129. /*
  1130. * Initialize the plugin
  1131. *
  1132. * uidunique_init (the old name) is deprecated
  1133. */
  1134. int
  1135. NSUniqueAttr_Init(Slapi_PBlock *pb)
  1136. {
  1137. int err = 0;
  1138. Slapi_Entry *plugin_entry = NULL;
  1139. char *plugin_type = NULL;
  1140. int preadd = SLAPI_PLUGIN_PRE_ADD_FN;
  1141. int premod = SLAPI_PLUGIN_PRE_MODIFY_FN;
  1142. int premdn = SLAPI_PLUGIN_PRE_MODRDN_FN;
  1143. BEGIN
  1144. /* Declare plugin version */
  1145. err = slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  1146. SLAPI_PLUGIN_VERSION_01);
  1147. if (err) break;
  1148. /*
  1149. * Get plugin identity and store it for later use
  1150. * Used for internal operations
  1151. */
  1152. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &plugin_identity);
  1153. /* PR_ASSERT (plugin_identity); */
  1154. if ((slapi_pblock_get(pb, SLAPI_PLUGIN_CONFIG_ENTRY, &plugin_entry) == 0) &&
  1155. plugin_entry &&
  1156. (plugin_type = slapi_entry_attr_get_charptr(plugin_entry, "nsslapd-plugintype")) &&
  1157. plugin_type && strstr(plugin_type, "betxn")) {
  1158. preadd = SLAPI_PLUGIN_BE_TXN_PRE_ADD_FN;
  1159. premod = SLAPI_PLUGIN_BE_TXN_PRE_MODIFY_FN;
  1160. premdn = SLAPI_PLUGIN_BE_TXN_PRE_MODRDN_FN;
  1161. }
  1162. slapi_ch_free_string(&plugin_type);
  1163. /* Provide descriptive information */
  1164. err = slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  1165. (void*)&pluginDesc);
  1166. if (err) break;
  1167. /* Register functions */
  1168. err = slapi_pblock_set(pb, preadd, (void*)preop_add);
  1169. if (err) break;
  1170. err = slapi_pblock_set(pb, premod, (void*)preop_modify);
  1171. if (err) break;
  1172. err = slapi_pblock_set(pb, premdn, (void*)preop_modrdn);
  1173. if (err) break;
  1174. err = slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN, (void *) uiduniq_start);
  1175. if (err) break;
  1176. err = slapi_pblock_set(pb, SLAPI_PLUGIN_CLOSE_FN, (void *) uiduniq_close);
  1177. if (err) break;
  1178. END
  1179. if (err) {
  1180. slapi_log_error(SLAPI_LOG_PLUGIN, "NSUniqueAttr_Init", "Error: %d\n", err);
  1181. err = -1;
  1182. }
  1183. else
  1184. slapi_log_error(SLAPI_LOG_PLUGIN, "NSUniqueAttr_Init", "plugin loaded\n");
  1185. return err;
  1186. }
  1187. /* ------------------------------------------------------------ */
  1188. /*
  1189. * ldap_quote_filter_value
  1190. *
  1191. * Quote the filter value according to RFC 2254 (Dec 1997)
  1192. *
  1193. * value - a UTF8 string containing the value. It may contain
  1194. * any of the chars needing quotes ( '(' ')' '*' '/' and NUL ).
  1195. * len - the length of the UTF8 value
  1196. * out - a buffer to recieve the converted value. May be NULL, in
  1197. * which case, only the length of the output is computed (and placed in
  1198. * outLen).
  1199. * maxLen - the size of the output buffer. It is an error if this length
  1200. * is exceeded. Ignored if out is NULL.
  1201. * outLen - recieves the size of the output. If an error occurs, this
  1202. * result is not available.
  1203. *
  1204. * Returns
  1205. * 0 - success
  1206. * -1 - failure (usually a buffer overflow)
  1207. */
  1208. int /* Error value */
  1209. ldap_quote_filter_value(
  1210. char *value, int len,
  1211. char *out, int maxLen,
  1212. int *outLen)
  1213. {
  1214. int err;
  1215. char *eValue;
  1216. int resLen;
  1217. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  1218. static char hexchars[16] = "0123456789abcdef";
  1219. #endif
  1220. err = 0;
  1221. eValue = &value[len];
  1222. resLen = 0;
  1223. /*
  1224. * Convert each character in the input string
  1225. */
  1226. while(value < eValue)
  1227. {
  1228. switch(*value)
  1229. {
  1230. case '(':
  1231. case ')':
  1232. case '*':
  1233. case '\\':
  1234. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  1235. case 0:
  1236. #endif
  1237. /* Handle characters needing special escape sequences */
  1238. /* Compute size of output */
  1239. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  1240. resLen += 3;
  1241. #else
  1242. resLen += 2;
  1243. #endif
  1244. /* Generate output if requested */
  1245. if (out)
  1246. {
  1247. /* Check for overflow */
  1248. if (resLen > maxLen) { err = -1; break; }
  1249. *out++ = '\\';
  1250. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  1251. *out++ = hexchars[(*value >> 4) & 0xF];
  1252. *out++ = hexchars[*value & 0xF];
  1253. #else
  1254. *out++ = *value;
  1255. #endif
  1256. }
  1257. break;
  1258. default:
  1259. /* Compute size of output */
  1260. resLen += 1;
  1261. /* Generate output if requested */
  1262. if (out)
  1263. {
  1264. if (resLen > maxLen) { err = -1; break; }
  1265. *out++ = *value;
  1266. }
  1267. break;
  1268. }
  1269. if (err) break;
  1270. value++;
  1271. }
  1272. if (!err) *outLen = resLen;
  1273. return err;
  1274. }