uid.c 51 KB

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