uid.c 53 KB

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