uid.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /*
  42. * uid.c
  43. *
  44. * Implements a directory server pre-operation plugin to test
  45. * attributes for uniqueness within a defined subtree in the
  46. * directory.
  47. *
  48. * Called uid.c since the original purpose of the plugin was to
  49. * check the uid attribute in user entries.
  50. */
  51. #include <slapi-plugin.h>
  52. #include <portable.h>
  53. #include <string.h>
  54. #include "plugin-utils.h"
  55. #include "nspr.h"
  56. #if defined( LDAP_DEBUG ) && !defined( DEBUG )
  57. #define DEBUG
  58. #endif
  59. #define UNTAGGED_PARAMETER 12
  60. /* Quoting routine - this should be in a library somewhere (slapi?) */
  61. int ldap_quote_filter_value(
  62. char *value, int len,
  63. char *out, int maxLen,
  64. int *outLen);
  65. static int search_one_berval(Slapi_DN *baseDN, const char *attrName,
  66. const struct berval *value, const char *requiredObjectClass, Slapi_DN *target);
  67. /*
  68. * ISSUES:
  69. * How should this plugin handle ACL issues? It seems wrong to reject
  70. * adds and modifies because there is already a conflicting UID, when
  71. * the request would have failed because of an ACL check anyway.
  72. *
  73. * This code currently defines a maximum filter string size of 512. Is
  74. * this large enough?
  75. *
  76. * This code currently does not quote the value portion of the filter as
  77. * it is created. This is a bug.
  78. */
  79. /* */
  80. #define BEGIN do {
  81. #define END } while(0);
  82. /*
  83. * Slapi plugin descriptor
  84. */
  85. static char *plugin_name = "NSUniqueAttr";
  86. static Slapi_PluginDesc
  87. pluginDesc = {
  88. "NSUniqueAttr", VENDOR, DS_PACKAGE_VERSION,
  89. "Enforce unique attribute values"
  90. };
  91. static void* plugin_identity = NULL;
  92. /*
  93. * More information about constraint failure
  94. */
  95. static char *moreInfo =
  96. "Another entry with the same attribute value already exists (attribute: \"%s\")";
  97. static void
  98. freePblock( Slapi_PBlock *spb ) {
  99. if ( spb )
  100. {
  101. slapi_free_search_results_internal( spb );
  102. slapi_pblock_destroy( spb );
  103. }
  104. }
  105. /* ------------------------------------------------------------ */
  106. /*
  107. * op_error - Record (and report) an operational error.
  108. * name changed to uid_op_error so as not to conflict with the external function
  109. * of the same name thereby preventing compiler warnings.
  110. */
  111. static int
  112. uid_op_error(int internal_error)
  113. {
  114. slapi_log_error(
  115. SLAPI_LOG_PLUGIN,
  116. plugin_name,
  117. "Internal error: %d\n",
  118. internal_error);
  119. return LDAP_OPERATIONS_ERROR;
  120. }
  121. /* ------------------------------------------------------------ */
  122. /*
  123. * Create an LDAP search filter from the attribute
  124. * name and value supplied.
  125. */
  126. static char *
  127. create_filter(const char *attribute, const struct berval *value, const char *requiredObjectClass)
  128. {
  129. char *filter;
  130. char *fp;
  131. char *max;
  132. int attrLen;
  133. int valueLen;
  134. int classLen = 0;
  135. int filterLen;
  136. PR_ASSERT(attribute);
  137. /* Compute the length of the required buffer */
  138. attrLen = strlen(attribute);
  139. if (ldap_quote_filter_value(value->bv_val,
  140. value->bv_len, 0, 0, &valueLen))
  141. return 0;
  142. if (requiredObjectClass) {
  143. classLen = strlen(requiredObjectClass);
  144. /* "(&(objectClass=)())" == 19 */
  145. filterLen = attrLen + 1 + valueLen + classLen + 19 + 1;
  146. } else {
  147. filterLen = attrLen + 1 + valueLen + 1;
  148. }
  149. /* Allocate the buffer */
  150. filter = slapi_ch_malloc(filterLen);
  151. fp = filter;
  152. max = &filter[filterLen];
  153. /* Place AND expression and objectClass in filter */
  154. if (requiredObjectClass) {
  155. strcpy(fp, "(&(objectClass=");
  156. fp += 15;
  157. strcpy(fp, requiredObjectClass);
  158. fp += classLen;
  159. *fp++ = ')';
  160. *fp++ = '(';
  161. }
  162. /* Place attribute name in filter */
  163. strcpy(fp, attribute);
  164. fp += attrLen;
  165. /* Place comparison operator */
  166. *fp++ = '=';
  167. /* Place value in filter */
  168. if (ldap_quote_filter_value(value->bv_val, value->bv_len,
  169. fp, max-fp, &valueLen)) { slapi_ch_free((void**)&filter); return 0; }
  170. fp += valueLen;
  171. /* Close AND expression if a requiredObjectClass was set */
  172. if (requiredObjectClass) {
  173. *fp++ = ')';
  174. *fp++ = ')';
  175. }
  176. /* Terminate */
  177. *fp = 0;
  178. return filter;
  179. }
  180. /* ------------------------------------------------------------ */
  181. /*
  182. * search - search a subtree for entries with a named attribute matching
  183. * the list of values. An entry matching the 'target' DN is
  184. * not considered in the search.
  185. *
  186. * If 'attr' is NULL, the values are taken from 'values'.
  187. * If 'attr' is non-NULL, the values are taken from 'attr'.
  188. *
  189. * Return:
  190. * LDAP_SUCCESS - no matches, or the attribute matches the
  191. * target dn.
  192. * LDAP_CONSTRAINT_VIOLATION - an entry was found that already
  193. * contains the attribute value.
  194. * LDAP_OPERATIONS_ERROR - a server failure.
  195. */
  196. static int
  197. search(Slapi_DN *baseDN, const char *attrName, Slapi_Attr *attr,
  198. struct berval **values, const char *requiredObjectClass,
  199. Slapi_DN *target)
  200. {
  201. int result;
  202. #ifdef DEBUG
  203. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  204. "SEARCH baseDN=%s attr=%s target=%s\n", slapi_sdn_get_dn(baseDN), attrName,
  205. target?slapi_sdn_get_dn(target):"None");
  206. #endif
  207. result = LDAP_SUCCESS;
  208. /* If no values, can't possibly be a conflict */
  209. if ( (Slapi_Attr *)NULL == attr && (struct berval **)NULL == values )
  210. return result;
  211. /*
  212. * Perform the search for each value provided
  213. *
  214. * Another possibility would be to search for all the values at once.
  215. * However, this is more complex (for filter creation) and unique
  216. * attributes values are probably only changed one at a time anyway.
  217. */
  218. if ( (Slapi_Attr *)NULL != attr )
  219. {
  220. Slapi_Value *v = NULL;
  221. int vhint = -1;
  222. for ( vhint = slapi_attr_first_value( attr, &v );
  223. vhint != -1 && LDAP_SUCCESS == result;
  224. vhint = slapi_attr_next_value( attr, vhint, &v ))
  225. {
  226. result = search_one_berval(baseDN, attrName,
  227. slapi_value_get_berval(v),
  228. requiredObjectClass, target);
  229. }
  230. }
  231. else
  232. {
  233. for (;*values != NULL && LDAP_SUCCESS == result; values++)
  234. {
  235. result = search_one_berval(baseDN, attrName, *values, requiredObjectClass,
  236. target);
  237. }
  238. }
  239. #ifdef DEBUG
  240. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  241. "SEARCH result = %d\n", result);
  242. #endif
  243. return( result );
  244. }
  245. static int
  246. search_one_berval(Slapi_DN *baseDN, const char *attrName,
  247. const struct berval *value, const char *requiredObjectClass,
  248. Slapi_DN *target)
  249. {
  250. int result;
  251. char *filter;
  252. Slapi_PBlock *spb;
  253. result = LDAP_SUCCESS;
  254. /* If no value, can't possibly be a conflict */
  255. if ( (struct berval *)NULL == value )
  256. return result;
  257. filter = 0;
  258. spb = 0;
  259. BEGIN
  260. int err;
  261. int sres;
  262. Slapi_Entry **entries;
  263. static char *attrs[] = { "1.1", 0 };
  264. /* Create the filter - this needs to be freed */
  265. filter = create_filter(attrName, value, requiredObjectClass);
  266. #ifdef DEBUG
  267. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  268. "SEARCH filter=%s\n", filter);
  269. #endif
  270. /* Perform the search using the new internal API */
  271. spb = slapi_pblock_new();
  272. if (!spb) { result = uid_op_error(2); break; }
  273. slapi_search_internal_set_pb_ext(spb, baseDN, LDAP_SCOPE_SUBTREE,
  274. filter, attrs, 0 /* attrs only */, NULL, NULL, plugin_identity, 0 /* actions */);
  275. slapi_search_internal_pb(spb);
  276. err = slapi_pblock_get(spb, SLAPI_PLUGIN_INTOP_RESULT, &sres);
  277. if (err) { result = uid_op_error(3); break; }
  278. /* Allow search to report that there is nothing in the subtree */
  279. if (sres == LDAP_NO_SUCH_OBJECT) break;
  280. /* Other errors are bad */
  281. if (sres) { result = uid_op_error(3); break; }
  282. err = slapi_pblock_get(spb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  283. &entries);
  284. if (err) { result = uid_op_error(4); break; }
  285. /*
  286. * Look at entries returned. Any entry found must be the
  287. * target entry or the constraint fails.
  288. */
  289. for(;*entries;entries++)
  290. {
  291. #ifdef DEBUG
  292. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  293. "SEARCH entry dn=%s\n", slapi_entry_get_dn(*entries));
  294. #endif
  295. /*
  296. * It is a Constraint Violation if any entry is found, unless
  297. * the entry is the target entry (if any).
  298. */
  299. if (!target || slapi_sdn_compare(slapi_entry_get_sdn(*entries), target) != 0)
  300. {
  301. result = LDAP_CONSTRAINT_VIOLATION;
  302. break;
  303. }
  304. }
  305. #ifdef DEBUG
  306. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  307. "SEARCH complete result=%d\n", result);
  308. #endif
  309. END
  310. /* Clean-up */
  311. if (spb) {
  312. slapi_free_search_results_internal(spb);
  313. slapi_pblock_destroy(spb);
  314. }
  315. slapi_ch_free((void**)&filter);
  316. return result;
  317. }
  318. /* ------------------------------------------------------------ */
  319. /*
  320. * searchAllSubtrees - search all subtrees in argv for entries
  321. * with a named attribute matching the list of values, by
  322. * calling search for each one.
  323. *
  324. * If 'attr' is NULL, the values are taken from 'values'.
  325. * If 'attr' is non-NULL, the values are taken from 'attr'.
  326. *
  327. * Return:
  328. * LDAP_SUCCESS - no matches, or the attribute matches the
  329. * target dn.
  330. * LDAP_CONSTRAINT_VIOLATION - an entry was found that already
  331. * contains the attribute value.
  332. * LDAP_OPERATIONS_ERROR - a server failure.
  333. */
  334. static int
  335. searchAllSubtrees(int argc, char *argv[], const char *attrName,
  336. Slapi_Attr *attr, struct berval **values, const char *requiredObjectClass,
  337. Slapi_DN *dn)
  338. {
  339. int result = LDAP_SUCCESS;
  340. /*
  341. * For each DN in the managed list, do uniqueness checking if
  342. * the target DN is a subnode in the tree.
  343. */
  344. for(;argc > 0;argc--,argv++)
  345. {
  346. Slapi_DN *sufdn = slapi_sdn_new_dn_byref(*argv);
  347. /*
  348. * The DN should already be normalized, so we don't have to
  349. * worry about that here.
  350. */
  351. if (slapi_sdn_issuffix(dn, sufdn)) {
  352. result = search(sufdn, attrName, attr, values, requiredObjectClass, dn);
  353. slapi_sdn_free(&sufdn);
  354. if (result) break;
  355. } else {
  356. slapi_sdn_free(&sufdn);
  357. }
  358. }
  359. return result;
  360. }
  361. /* ------------------------------------------------------------ */
  362. /*
  363. * getArguments - parse invocation parameters
  364. * Return:
  365. * 0 - success
  366. * >0 - error parsing parameters
  367. */
  368. static int
  369. getArguments(Slapi_PBlock *pb, char **attrName, char **markerObjectClass,
  370. char **requiredObjectClass)
  371. {
  372. int argc;
  373. char **argv;
  374. /*
  375. * Get the arguments
  376. */
  377. if (slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc))
  378. {
  379. return uid_op_error(10);
  380. }
  381. if (slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv))
  382. {
  383. return uid_op_error(11);
  384. }
  385. /*
  386. * Required arguments: attribute and markerObjectClass
  387. * Optional argument: requiredObjectClass
  388. */
  389. for(;argc > 0;argc--,argv++)
  390. {
  391. char *param = *argv;
  392. char *delimiter = strchr(param, '=');
  393. if (NULL == delimiter)
  394. {
  395. /* Old style untagged parameter */
  396. *attrName = *argv;
  397. return UNTAGGED_PARAMETER;
  398. }
  399. if (strncasecmp(param, "attribute", delimiter-param) == 0)
  400. {
  401. /* It's OK to set a pointer here, because ultimately it points
  402. * inside the argv array of the pblock, which will be staying
  403. * arround.
  404. */
  405. *attrName = delimiter+1;
  406. } else if (strncasecmp(param, "markerobjectclass", delimiter-param) == 0)
  407. {
  408. *markerObjectClass = delimiter+1;
  409. } else if (strncasecmp(param, "requiredobjectclass", delimiter-param) == 0)
  410. {
  411. *requiredObjectClass = delimiter+1;
  412. }
  413. }
  414. if (!*attrName || !*markerObjectClass)
  415. {
  416. return uid_op_error(13);
  417. }
  418. return 0;
  419. }
  420. /* ------------------------------------------------------------ */
  421. /*
  422. * findSubtreeAndSearch - walk up the tree to find an entry with
  423. * the marker object class; if found, call search from there and
  424. * return the result it returns
  425. *
  426. * If 'attr' is NULL, the values are taken from 'values'.
  427. * If 'attr' is non-NULL, the values are taken from 'attr'.
  428. *
  429. * Return:
  430. * LDAP_SUCCESS - no matches, or the attribute matches the
  431. * target dn.
  432. * LDAP_CONSTRAINT_VIOLATION - an entry was found that already
  433. * contains the attribute value.
  434. * LDAP_OPERATIONS_ERROR - a server failure.
  435. */
  436. static int
  437. findSubtreeAndSearch(Slapi_DN *parentDN, const char *attrName, Slapi_Attr *attr,
  438. struct berval **values, const char *requiredObjectClass, Slapi_DN *target,
  439. const char *markerObjectClass)
  440. {
  441. int result = LDAP_SUCCESS;
  442. Slapi_PBlock *spb = NULL;
  443. Slapi_DN *curpar = slapi_sdn_new();
  444. Slapi_DN *newpar = NULL;
  445. slapi_sdn_get_parent(parentDN, curpar);
  446. while ((curpar != NULL) && (slapi_sdn_get_dn(curpar) != NULL))
  447. {
  448. if ((spb = dnHasObjectClass(curpar, markerObjectClass)))
  449. {
  450. freePblock(spb);
  451. /*
  452. * Do the search. There is no entry that is allowed
  453. * to have the attribute already.
  454. */
  455. result = search(curpar, attrName, attr, values, requiredObjectClass,
  456. target);
  457. break;
  458. }
  459. newpar = slapi_sdn_new();
  460. slapi_sdn_copy(curpar, newpar);
  461. slapi_sdn_get_parent(newpar, curpar);
  462. slapi_sdn_free(&newpar);
  463. }
  464. slapi_sdn_free(&curpar);
  465. return result;
  466. }
  467. /* ------------------------------------------------------------ */
  468. /*
  469. * preop_add - pre-operation plug-in for add
  470. */
  471. static int
  472. preop_add(Slapi_PBlock *pb)
  473. {
  474. int result;
  475. char *errtext = NULL;
  476. char *attrName = NULL;
  477. #ifdef DEBUG
  478. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name, "ADD begin\n");
  479. #endif
  480. result = LDAP_SUCCESS;
  481. /*
  482. * Do constraint check on the added entry. Set result.
  483. */
  484. BEGIN
  485. int err;
  486. char *markerObjectClass = NULL;
  487. char *requiredObjectClass = NULL;
  488. Slapi_DN *sdn = NULL;
  489. int isupdatedn;
  490. Slapi_Entry *e;
  491. Slapi_Attr *attr;
  492. int argc;
  493. char **argv = NULL;
  494. /*
  495. * If this is a replication update, just be a noop.
  496. */
  497. err = slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &isupdatedn);
  498. if (err) { result = uid_op_error(50); break; }
  499. if (isupdatedn)
  500. {
  501. break;
  502. }
  503. /*
  504. * Get the arguments
  505. */
  506. result = getArguments(pb, &attrName, &markerObjectClass,
  507. &requiredObjectClass);
  508. if (UNTAGGED_PARAMETER == result)
  509. {
  510. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  511. "ADD parameter untagged: %s\n", attrName);
  512. result = LDAP_SUCCESS;
  513. /* Statically defined subtrees to monitor */
  514. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc);
  515. if (err) { result = uid_op_error(53); break; }
  516. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv);
  517. if (err) { result = uid_op_error(54); break; }
  518. argc--; argv++; /* First argument was attribute name */
  519. } else if (0 != result)
  520. {
  521. break;
  522. }
  523. /*
  524. * Get the target DN for this add operation
  525. */
  526. err = slapi_pblock_get(pb, SLAPI_ADD_TARGET_SDN, &sdn);
  527. if (err) { result = uid_op_error(51); break; }
  528. #ifdef DEBUG
  529. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name, "ADD target=%s\n", slapi_sdn_get_dn(sdn));
  530. #endif
  531. /*
  532. * Get the entry data for this add. Check whether it
  533. * contains a value for the unique attribute
  534. */
  535. err = slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e);
  536. if (err) { result = uid_op_error(52); break; }
  537. err = slapi_entry_attr_find(e, attrName, &attr);
  538. if (err) break; /* no unique attribute */
  539. /*
  540. * Check if it contains the required object class
  541. */
  542. if (NULL != requiredObjectClass)
  543. {
  544. if (!entryHasObjectClass(pb, e, requiredObjectClass))
  545. {
  546. /* No, so we don't have to do anything */
  547. break;
  548. }
  549. }
  550. /*
  551. * Passed all the requirements - this is an operation we
  552. * need to enforce uniqueness on. Now find all parent entries
  553. * with the marker object class, and do a search for each one.
  554. */
  555. if (NULL != markerObjectClass)
  556. {
  557. /* Subtree defined by location of marker object class */
  558. result = findSubtreeAndSearch(sdn, attrName, attr, NULL,
  559. requiredObjectClass, sdn,
  560. markerObjectClass);
  561. } else
  562. {
  563. /* Subtrees listed on invocation line */
  564. result = searchAllSubtrees(argc, argv, attrName, attr, NULL,
  565. requiredObjectClass, sdn);
  566. }
  567. END
  568. if (result)
  569. {
  570. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  571. "ADD result %d\n", result);
  572. if (result == LDAP_CONSTRAINT_VIOLATION) {
  573. errtext = slapi_ch_smprintf(moreInfo, attrName);
  574. } else {
  575. errtext = slapi_ch_strdup("Error checking for attribute uniqueness.");
  576. }
  577. /* Send failure to the client */
  578. slapi_send_ldap_result(pb, result, 0, errtext, 0, 0);
  579. slapi_ch_free_string(&errtext);
  580. }
  581. return (result==LDAP_SUCCESS)?0:-1;
  582. }
  583. static void
  584. addMod(LDAPMod ***modary, int *capacity, int *nmods, LDAPMod *toadd)
  585. {
  586. if (*nmods == *capacity) {
  587. *capacity += 4;
  588. if (*modary) {
  589. *modary = (LDAPMod **)slapi_ch_realloc((char *)*modary, *capacity * sizeof(LDAPMod *));
  590. } else {
  591. *modary = (LDAPMod **)slapi_ch_malloc(*capacity * sizeof(LDAPMod *));
  592. }
  593. }
  594. (*modary)[*nmods] = toadd;
  595. (*nmods)++;
  596. }
  597. /* ------------------------------------------------------------ */
  598. /*
  599. * preop_modify - pre-operation plug-in for modify
  600. */
  601. static int
  602. preop_modify(Slapi_PBlock *pb)
  603. {
  604. int result = LDAP_SUCCESS;
  605. Slapi_PBlock *spb = NULL;
  606. LDAPMod **checkmods = NULL;
  607. int checkmodsCapacity = 0;
  608. char *errtext = NULL;
  609. char *attrName = NULL;
  610. #ifdef DEBUG
  611. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  612. "MODIFY begin\n");
  613. #endif
  614. BEGIN
  615. int err;
  616. char *markerObjectClass=NULL;
  617. char *requiredObjectClass=NULL;
  618. LDAPMod **mods;
  619. int modcount = 0;
  620. int ii;
  621. LDAPMod *mod;
  622. Slapi_DN *sdn = NULL;
  623. int isupdatedn;
  624. int argc;
  625. char **argv = NULL;
  626. /*
  627. * If this is a replication update, just be a noop.
  628. */
  629. err = slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &isupdatedn);
  630. if (err) { result = uid_op_error(60); break; }
  631. if (isupdatedn)
  632. {
  633. break;
  634. }
  635. /*
  636. * Get the arguments
  637. */
  638. result = getArguments(pb, &attrName, &markerObjectClass,
  639. &requiredObjectClass);
  640. if (UNTAGGED_PARAMETER == result)
  641. {
  642. result = LDAP_SUCCESS;
  643. /* Statically defined subtrees to monitor */
  644. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc);
  645. if (err) { result = uid_op_error(53); break; }
  646. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv);
  647. if (err) { result = uid_op_error(54); break; }
  648. argc--; /* First argument was attribute name */
  649. argv++;
  650. } else if (0 != result)
  651. {
  652. break;
  653. }
  654. err = slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  655. if (err) { result = uid_op_error(61); break; }
  656. /* There may be more than one mod that matches e.g.
  657. changetype: modify
  658. delete: uid
  659. uid: balster1950
  660. -
  661. add: uid
  662. uid: scottg
  663. So, we need to first find all mods that contain the attribute
  664. which are add or replace ops and are bvalue encoded
  665. */
  666. /* find out how many mods meet this criteria */
  667. for(;*mods;mods++)
  668. {
  669. mod = *mods;
  670. if ((slapi_attr_type_cmp(mod->mod_type, attrName, 1) == 0) && /* mod contains target attr */
  671. (mod->mod_op & LDAP_MOD_BVALUES) && /* mod is bval encoded (not string val) */
  672. (mod->mod_bvalues && mod->mod_bvalues[0]) && /* mod actually contains some values */
  673. (SLAPI_IS_MOD_ADD(mod->mod_op) || /* mod is add */
  674. SLAPI_IS_MOD_REPLACE(mod->mod_op))) /* mod is replace */
  675. {
  676. addMod(&checkmods, &checkmodsCapacity, &modcount, mod);
  677. }
  678. }
  679. if (modcount == 0) {
  680. break; /* no mods to check, we are done */
  681. }
  682. /* Get the target DN */
  683. err = slapi_pblock_get(pb, SLAPI_MODIFY_TARGET_SDN, &sdn);
  684. if (err) { result = uid_op_error(11); break; }
  685. /*
  686. * Check if it has the required object class
  687. */
  688. if (requiredObjectClass &&
  689. !(spb = dnHasObjectClass(sdn, requiredObjectClass))) {
  690. break;
  691. }
  692. /*
  693. * Passed all the requirements - this is an operation we
  694. * need to enforce uniqueness on. Now find all parent entries
  695. * with the marker object class, and do a search for each one.
  696. */
  697. /*
  698. * stop checking at first mod that fails the check
  699. */
  700. for (ii = 0; (result == 0) && (ii < modcount); ++ii)
  701. {
  702. mod = checkmods[ii];
  703. if (NULL != markerObjectClass)
  704. {
  705. /* Subtree defined by location of marker object class */
  706. result = findSubtreeAndSearch(sdn, attrName, NULL,
  707. mod->mod_bvalues, requiredObjectClass,
  708. sdn, markerObjectClass);
  709. } else
  710. {
  711. /* Subtrees listed on invocation line */
  712. result = searchAllSubtrees(argc, argv, attrName, NULL,
  713. mod->mod_bvalues, requiredObjectClass, sdn);
  714. }
  715. }
  716. END
  717. slapi_ch_free((void **)&checkmods);
  718. freePblock(spb);
  719. if (result)
  720. {
  721. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  722. "MODIFY result %d\n", result);
  723. if (result == LDAP_CONSTRAINT_VIOLATION) {
  724. errtext = slapi_ch_smprintf(moreInfo, attrName);
  725. } else {
  726. errtext = slapi_ch_strdup("Error checking for attribute uniqueness.");
  727. }
  728. slapi_send_ldap_result(pb, result, 0, errtext, 0, 0);
  729. slapi_ch_free_string(&errtext);
  730. }
  731. return (result==LDAP_SUCCESS)?0:-1;
  732. }
  733. /* ------------------------------------------------------------ */
  734. /*
  735. * preop_modrdn - Pre-operation call for modify RDN
  736. *
  737. * Check that the new RDN does not include attributes that
  738. * cause a constraint violation
  739. */
  740. static int
  741. preop_modrdn(Slapi_PBlock *pb)
  742. {
  743. int result = LDAP_SUCCESS;
  744. Slapi_Entry *e = NULL;
  745. Slapi_Value *sv_requiredObjectClass = NULL;
  746. char *errtext = NULL;
  747. char *attrName = NULL;
  748. #ifdef DEBUG
  749. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  750. "MODRDN begin\n");
  751. #endif
  752. BEGIN
  753. int err;
  754. char *markerObjectClass=NULL;
  755. char *requiredObjectClass=NULL;
  756. const char *dn = NULL;
  757. Slapi_DN *sdn = NULL;
  758. Slapi_DN *superior;
  759. char *rdn;
  760. int deloldrdn = 0;
  761. int isupdatedn;
  762. Slapi_Attr *attr;
  763. int argc;
  764. char **argv = NULL;
  765. /*
  766. * If this is a replication update, just be a noop.
  767. */
  768. err = slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &isupdatedn);
  769. if (err) { result = uid_op_error(30); break; }
  770. if (isupdatedn)
  771. {
  772. break;
  773. }
  774. /*
  775. * Get the arguments
  776. */
  777. result = getArguments(pb, &attrName, &markerObjectClass,
  778. &requiredObjectClass);
  779. if (UNTAGGED_PARAMETER == result)
  780. {
  781. result = LDAP_SUCCESS;
  782. /* Statically defined subtrees to monitor */
  783. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc);
  784. if (err) { result = uid_op_error(53); break; }
  785. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv);
  786. if (err) { result = uid_op_error(54); break; }
  787. argc--; /* First argument was attribute name */
  788. argv++;
  789. } else if (0 != result)
  790. {
  791. break;
  792. }
  793. /* Create a Slapi_Value for the requiredObjectClass to use
  794. * for checking the entry. */
  795. if (requiredObjectClass) {
  796. sv_requiredObjectClass = slapi_value_new_string(requiredObjectClass);
  797. }
  798. /* Get the DN of the entry being renamed */
  799. err = slapi_pblock_get(pb, SLAPI_MODRDN_TARGET_SDN, &sdn);
  800. if (err) { result = uid_op_error(31); break; }
  801. dn = slapi_sdn_get_dn(sdn);
  802. /* Get superior value - unimplemented in 3.0/4.0/5.0 DS */
  803. err = slapi_pblock_get(pb, SLAPI_MODRDN_NEWSUPERIOR_SDN, &superior);
  804. if (err) { result = uid_op_error(32); break; }
  805. /*
  806. * No superior means the entry is just renamed at
  807. * its current level in the tree. Use the target DN for
  808. * determining which managed tree this belongs to
  809. */
  810. if (!superior) superior = sdn;
  811. /* Get the new RDN - this has the attribute values */
  812. err = slapi_pblock_get(pb, SLAPI_MODRDN_NEWRDN, &rdn);
  813. if (err) { result = uid_op_error(33); break; }
  814. #ifdef DEBUG
  815. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  816. "MODRDN newrdn=%s\n", rdn);
  817. #endif
  818. /* See if the old RDN value is being deleted. */
  819. err = slapi_pblock_get(pb, SLAPI_MODRDN_DELOLDRDN, &deloldrdn);
  820. if (err) { result = uid_op_error(34); break; }
  821. /* Get the entry that is being renamed so we can make a dummy copy
  822. * of what it will look like after the rename. */
  823. err = slapi_search_internal_get_entry(sdn, NULL, &e, plugin_identity);
  824. if (err != LDAP_SUCCESS) {
  825. result = uid_op_error(35);
  826. /* We want to return a no such object error if the target doesn't exist. */
  827. if (err == LDAP_NO_SUCH_OBJECT) {
  828. result = err;
  829. }
  830. break;
  831. }
  832. /* Apply the rename operation to the dummy entry. */
  833. /* slapi_entry_rename does not expect rdn normalized */
  834. err = slapi_entry_rename(e, rdn, deloldrdn, superior);
  835. if (err != LDAP_SUCCESS) { result = uid_op_error(36); break; }
  836. /*
  837. * Find any unique attribute data in the new RDN
  838. */
  839. err = slapi_entry_attr_find(e, attrName, &attr);
  840. if (err) break; /* no UID attribute */
  841. /*
  842. * Check if it has the required object class
  843. */
  844. if (requiredObjectClass &&
  845. !slapi_entry_attr_has_syntax_value(e, SLAPI_ATTR_OBJECTCLASS, sv_requiredObjectClass)) { break; }
  846. /*
  847. * Passed all the requirements - this is an operation we
  848. * need to enforce uniqueness on. Now find all parent entries
  849. * with the marker object class, and do a search for each one.
  850. */
  851. if (NULL != markerObjectClass)
  852. {
  853. /* Subtree defined by location of marker object class */
  854. result = findSubtreeAndSearch(slapi_entry_get_sdn(e), attrName, attr, NULL,
  855. requiredObjectClass, sdn,
  856. markerObjectClass);
  857. } else
  858. {
  859. /* Subtrees listed on invocation line */
  860. result = searchAllSubtrees(argc, argv, attrName, attr, NULL,
  861. requiredObjectClass, sdn);
  862. }
  863. END
  864. /* Clean-up */
  865. slapi_value_free(&sv_requiredObjectClass);
  866. if (e) slapi_entry_free(e);
  867. if (result)
  868. {
  869. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  870. "MODRDN result %d\n", result);
  871. if (result == LDAP_CONSTRAINT_VIOLATION) {
  872. errtext = slapi_ch_smprintf(moreInfo, attrName);
  873. } else {
  874. errtext = slapi_ch_strdup("Error checking for attribute uniqueness.");
  875. }
  876. slapi_send_ldap_result(pb, result, 0, errtext, 0, 0);
  877. slapi_ch_free_string(&errtext);
  878. }
  879. return (result==LDAP_SUCCESS)?0:-1;
  880. }
  881. /* ------------------------------------------------------------ */
  882. /*
  883. * Initialize the plugin
  884. *
  885. * uidunique_init (the old name) is deprecated
  886. */
  887. int
  888. NSUniqueAttr_Init(Slapi_PBlock *pb)
  889. {
  890. int err = 0;
  891. Slapi_Entry *plugin_entry = NULL;
  892. char *plugin_type = NULL;
  893. int preadd = SLAPI_PLUGIN_PRE_ADD_FN;
  894. int premod = SLAPI_PLUGIN_PRE_MODIFY_FN;
  895. int premdn = SLAPI_PLUGIN_PRE_MODRDN_FN;
  896. BEGIN
  897. int argc;
  898. char **argv;
  899. /* Declare plugin version */
  900. err = slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  901. SLAPI_PLUGIN_VERSION_01);
  902. if (err) break;
  903. /*
  904. * Get plugin identity and store it for later use
  905. * Used for internal operations
  906. */
  907. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &plugin_identity);
  908. /* PR_ASSERT (plugin_identity); */
  909. if ((slapi_pblock_get(pb, SLAPI_PLUGIN_CONFIG_ENTRY, &plugin_entry) == 0) &&
  910. plugin_entry &&
  911. (plugin_type = slapi_entry_attr_get_charptr(plugin_entry, "nsslapd-plugintype")) &&
  912. plugin_type && strstr(plugin_type, "betxn")) {
  913. preadd = SLAPI_PLUGIN_BE_TXN_PRE_ADD_FN;
  914. premod = SLAPI_PLUGIN_BE_TXN_PRE_MODIFY_FN;
  915. premdn = SLAPI_PLUGIN_BE_TXN_PRE_MODRDN_FN;
  916. }
  917. slapi_ch_free_string(&plugin_type);
  918. /*
  919. * Get and normalize arguments
  920. */
  921. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc);
  922. if (err) break;
  923. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv);
  924. if (err) break;
  925. /* First argument is the unique attribute name */
  926. if (argc < 1) { err = -1; break; }
  927. argv++; argc--;
  928. for(;argc > 0;argc--, argv++) {
  929. char *normdn = slapi_create_dn_string_case("%s", *argv);
  930. slapi_ch_free_string(argv);
  931. *argv = normdn;
  932. }
  933. /* Provide descriptive information */
  934. err = slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  935. (void*)&pluginDesc);
  936. if (err) break;
  937. /* Register functions */
  938. err = slapi_pblock_set(pb, preadd, (void*)preop_add);
  939. if (err) break;
  940. err = slapi_pblock_set(pb, premod, (void*)preop_modify);
  941. if (err) break;
  942. err = slapi_pblock_set(pb, premdn, (void*)preop_modrdn);
  943. if (err) break;
  944. END
  945. if (err) {
  946. slapi_log_error(SLAPI_LOG_PLUGIN, "NSUniqueAttr_Init",
  947. "Error: %d\n", err);
  948. err = -1;
  949. }
  950. else
  951. slapi_log_error(SLAPI_LOG_PLUGIN, "NSUniqueAttr_Init",
  952. "plugin loaded\n");
  953. return err;
  954. }
  955. int
  956. uidunique_init(Slapi_PBlock *pb)
  957. {
  958. return NSUniqueAttr_Init(pb);
  959. }
  960. /* ------------------------------------------------------------ */
  961. /*
  962. * ldap_quote_filter_value
  963. *
  964. * Quote the filter value according to RFC 2254 (Dec 1997)
  965. *
  966. * value - a UTF8 string containing the value. It may contain
  967. * any of the chars needing quotes ( '(' ')' '*' '/' and NUL ).
  968. * len - the length of the UTF8 value
  969. * out - a buffer to recieve the converted value. May be NULL, in
  970. * which case, only the length of the output is computed (and placed in
  971. * outLen).
  972. * maxLen - the size of the output buffer. It is an error if this length
  973. * is exceeded. Ignored if out is NULL.
  974. * outLen - recieves the size of the output. If an error occurs, this
  975. * result is not available.
  976. *
  977. * Returns
  978. * 0 - success
  979. * -1 - failure (usually a buffer overflow)
  980. */
  981. int /* Error value */
  982. ldap_quote_filter_value(
  983. char *value, int len,
  984. char *out, int maxLen,
  985. int *outLen)
  986. {
  987. int err;
  988. char *eValue;
  989. int resLen;
  990. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  991. static char hexchars[16] = "0123456789abcdef";
  992. #endif
  993. err = 0;
  994. eValue = &value[len];
  995. resLen = 0;
  996. /*
  997. * Convert each character in the input string
  998. */
  999. while(value < eValue)
  1000. {
  1001. switch(*value)
  1002. {
  1003. case '(':
  1004. case ')':
  1005. case '*':
  1006. case '\\':
  1007. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  1008. case 0:
  1009. #endif
  1010. /* Handle characters needing special escape sequences */
  1011. /* Compute size of output */
  1012. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  1013. resLen += 3;
  1014. #else
  1015. resLen += 2;
  1016. #endif
  1017. /* Generate output if requested */
  1018. if (out)
  1019. {
  1020. /* Check for overflow */
  1021. if (resLen > maxLen) { err = -1; break; }
  1022. *out++ = '\\';
  1023. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  1024. *out++ = hexchars[(*value >> 4) & 0xF];
  1025. *out++ = hexchars[*value & 0xF];
  1026. #else
  1027. *out++ = *value;
  1028. #endif
  1029. }
  1030. break;
  1031. default:
  1032. /* Compute size of output */
  1033. resLen += 1;
  1034. /* Generate output if requested */
  1035. if (out)
  1036. {
  1037. if (resLen > maxLen) { err = -1; break; }
  1038. *out++ = *value;
  1039. }
  1040. break;
  1041. }
  1042. if (err) break;
  1043. value++;
  1044. }
  1045. if (!err) *outLen = resLen;
  1046. return err;
  1047. }