uid.c 50 KB

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