uid.c 53 KB

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