uid.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  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(const char *baseDN, const char *attrName,
  66. const struct berval *value, const char *requiredObjectClass, const char *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";
  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(const char *baseDN, const char *attrName, Slapi_Attr *attr,
  198. struct berval **values, const char *requiredObjectClass,
  199. const char *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", baseDN, attrName,
  205. target?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(const char *baseDN, const char *attrName,
  247. const struct berval *value, const char *requiredObjectClass,
  248. const char *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(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. char *dn = slapi_entry_get_dn(*entries);
  292. /*
  293. * DNs are returned in the original value used to insert
  294. * the entry. This must be "normalized" for comparison.
  295. *
  296. * This normalization is done "in-place" (modifying the value
  297. * in the entry). This is OK, since this is the only user
  298. * of this copy of the entry.
  299. */
  300. slapi_dn_normalize_case(dn);
  301. #ifdef DEBUG
  302. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  303. "SEARCH entry dn=%s\n", dn);
  304. #endif
  305. /*
  306. * It is a Constraint Violation if any entry is found, unless
  307. * the entry is the target entry (if any).
  308. */
  309. if (!target || strcmp(dn, target) != 0)
  310. {
  311. result = LDAP_CONSTRAINT_VIOLATION;
  312. break;
  313. }
  314. }
  315. #ifdef DEBUG
  316. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  317. "SEARCH complete result=%d\n", result);
  318. #endif
  319. END
  320. /* Clean-up */
  321. if (spb) {
  322. slapi_free_search_results_internal(spb);
  323. slapi_pblock_destroy(spb);
  324. }
  325. slapi_ch_free((void**)&filter);
  326. return result;
  327. }
  328. /* ------------------------------------------------------------ */
  329. /*
  330. * searchAllSubtrees - search all subtrees in argv for entries
  331. * with a named attribute matching the list of values, by
  332. * calling search for each one.
  333. *
  334. * If 'attr' is NULL, the values are taken from 'values'.
  335. * If 'attr' is non-NULL, the values are taken from 'attr'.
  336. *
  337. * Return:
  338. * LDAP_SUCCESS - no matches, or the attribute matches the
  339. * target dn.
  340. * LDAP_CONSTRAINT_VIOLATION - an entry was found that already
  341. * contains the attribute value.
  342. * LDAP_OPERATIONS_ERROR - a server failure.
  343. */
  344. static int
  345. searchAllSubtrees(int argc, char *argv[], const char *attrName,
  346. Slapi_Attr *attr, struct berval **values, const char *requiredObjectClass,
  347. const char *dn)
  348. {
  349. int result = LDAP_SUCCESS;
  350. /*
  351. * For each DN in the managed list, do uniqueness checking if
  352. * the target DN is a subnode in the tree.
  353. */
  354. for(;argc > 0;argc--,argv++)
  355. {
  356. /*
  357. * The DN should already be normalized, so we don't have to
  358. * worry about that here.
  359. */
  360. if (slapi_dn_issuffix(dn, *argv)) {
  361. result = search(*argv, attrName, attr, values, requiredObjectClass, dn);
  362. if (result) break;
  363. }
  364. }
  365. return result;
  366. }
  367. /* ------------------------------------------------------------ */
  368. /*
  369. * getArguments - parse invocation parameters
  370. * Return:
  371. * 0 - success
  372. * >0 - error parsing parameters
  373. */
  374. static int
  375. getArguments(Slapi_PBlock *pb, char **attrName, char **markerObjectClass,
  376. char **requiredObjectClass)
  377. {
  378. int argc;
  379. char **argv;
  380. /*
  381. * Get the arguments
  382. */
  383. if (slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc))
  384. {
  385. return uid_op_error(10);
  386. }
  387. if (slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv))
  388. {
  389. return uid_op_error(11);
  390. }
  391. /*
  392. * Required arguments: attribute and markerObjectClass
  393. * Optional argument: requiredObjectClass
  394. */
  395. for(;argc > 0;argc--,argv++)
  396. {
  397. char *param = *argv;
  398. char *delimiter = strchr(param, '=');
  399. if (NULL == delimiter)
  400. {
  401. /* Old style untagged parameter */
  402. *attrName = *argv;
  403. return UNTAGGED_PARAMETER;
  404. }
  405. if (strncasecmp(param, "attribute", delimiter-param) == 0)
  406. {
  407. /* It's OK to set a pointer here, because ultimately it points
  408. * inside the argv array of the pblock, which will be staying
  409. * arround.
  410. */
  411. *attrName = delimiter+1;
  412. } else if (strncasecmp(param, "markerobjectclass", delimiter-param) == 0)
  413. {
  414. *markerObjectClass = delimiter+1;
  415. } else if (strncasecmp(param, "requiredobjectclass", delimiter-param) == 0)
  416. {
  417. *requiredObjectClass = delimiter+1;
  418. }
  419. }
  420. if (!*attrName || !*markerObjectClass)
  421. {
  422. return uid_op_error(13);
  423. }
  424. return 0;
  425. }
  426. /* ------------------------------------------------------------ */
  427. /*
  428. * findSubtreeAndSearch - walk up the tree to find an entry with
  429. * the marker object class; if found, call search from there and
  430. * return the result it returns
  431. *
  432. * If 'attr' is NULL, the values are taken from 'values'.
  433. * If 'attr' is non-NULL, the values are taken from 'attr'.
  434. *
  435. * Return:
  436. * LDAP_SUCCESS - no matches, or the attribute matches the
  437. * target dn.
  438. * LDAP_CONSTRAINT_VIOLATION - an entry was found that already
  439. * contains the attribute value.
  440. * LDAP_OPERATIONS_ERROR - a server failure.
  441. */
  442. static int
  443. findSubtreeAndSearch(char *parentDN, const char *attrName, Slapi_Attr *attr,
  444. struct berval **values, const char *requiredObjectClass, const char *target,
  445. const char *markerObjectClass)
  446. {
  447. int result = LDAP_SUCCESS;
  448. Slapi_PBlock *spb = NULL;
  449. while (NULL != (parentDN = slapi_dn_parent(parentDN)))
  450. {
  451. if ((spb = dnHasObjectClass(parentDN, markerObjectClass)))
  452. {
  453. freePblock(spb);
  454. /*
  455. * Do the search. There is no entry that is allowed
  456. * to have the attribute already.
  457. */
  458. result = search(parentDN, attrName, attr, values, requiredObjectClass,
  459. target);
  460. break;
  461. }
  462. }
  463. return result;
  464. }
  465. /* ------------------------------------------------------------ */
  466. /*
  467. * preop_add - pre-operation plug-in for add
  468. */
  469. static int
  470. preop_add(Slapi_PBlock *pb)
  471. {
  472. int result;
  473. #ifdef DEBUG
  474. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name, "ADD begin\n");
  475. #endif
  476. result = LDAP_SUCCESS;
  477. /*
  478. * Do constraint check on the added entry. Set result.
  479. */
  480. BEGIN
  481. int err;
  482. char *attrName = NULL;
  483. char *markerObjectClass = NULL;
  484. char *requiredObjectClass = NULL;
  485. char *dn;
  486. int isupdatedn;
  487. Slapi_Entry *e;
  488. Slapi_Attr *attr;
  489. int argc;
  490. char **argv = NULL;
  491. /*
  492. * If this is a replication update, just be a noop.
  493. */
  494. err = slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &isupdatedn);
  495. if (err) { result = uid_op_error(50); break; }
  496. if (isupdatedn)
  497. {
  498. break;
  499. }
  500. /*
  501. * Get the arguments
  502. */
  503. result = getArguments(pb, &attrName, &markerObjectClass,
  504. &requiredObjectClass);
  505. if (UNTAGGED_PARAMETER == result)
  506. {
  507. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  508. "ADD parameter untagged: %s\n", attrName);
  509. result = LDAP_SUCCESS;
  510. /* Statically defined subtrees to monitor */
  511. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc);
  512. if (err) { result = uid_op_error(53); break; }
  513. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv);
  514. if (err) { result = uid_op_error(54); break; }
  515. argc--; argv++; /* First argument was attribute name */
  516. } else if (0 != result)
  517. {
  518. break;
  519. }
  520. /*
  521. * Get the target DN for this add operation
  522. */
  523. err = slapi_pblock_get(pb, SLAPI_ADD_TARGET, &dn);
  524. if (err) { result = uid_op_error(51); break; }
  525. #ifdef DEBUG
  526. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name, "ADD target=%s\n", dn);
  527. #endif
  528. /*
  529. * Get the entry data for this add. Check whether it
  530. * contains a value for the unique attribute
  531. */
  532. err = slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e);
  533. if (err) { result = uid_op_error(52); break; }
  534. err = slapi_entry_attr_find(e, attrName, &attr);
  535. if (err) break; /* no unique attribute */
  536. /*
  537. * Check if it contains the required object class
  538. */
  539. if (NULL != requiredObjectClass)
  540. {
  541. if (!entryHasObjectClass(pb, e, requiredObjectClass))
  542. {
  543. /* No, so we don't have to do anything */
  544. break;
  545. }
  546. }
  547. /*
  548. * Passed all the requirements - this is an operation we
  549. * need to enforce uniqueness on. Now find all parent entries
  550. * with the marker object class, and do a search for each one.
  551. */
  552. if (NULL != markerObjectClass)
  553. {
  554. /* Subtree defined by location of marker object class */
  555. result = findSubtreeAndSearch(dn, attrName, attr, NULL,
  556. requiredObjectClass, dn,
  557. markerObjectClass);
  558. } else
  559. {
  560. /* Subtrees listed on invocation line */
  561. result = searchAllSubtrees(argc, argv, attrName, attr, NULL,
  562. requiredObjectClass, dn);
  563. }
  564. END
  565. if (result)
  566. {
  567. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  568. "ADD result %d\n", result);
  569. /* Send failure to the client */
  570. slapi_send_ldap_result(pb, result, 0, moreInfo, 0, 0);
  571. }
  572. return (result==LDAP_SUCCESS)?0:-1;
  573. }
  574. static void
  575. addMod(LDAPMod ***modary, int *capacity, int *nmods, LDAPMod *toadd)
  576. {
  577. if (*nmods == *capacity) {
  578. *capacity += 4;
  579. if (*modary) {
  580. *modary = (LDAPMod **)slapi_ch_realloc((char *)*modary, *capacity * sizeof(LDAPMod *));
  581. } else {
  582. *modary = (LDAPMod **)slapi_ch_malloc(*capacity * sizeof(LDAPMod *));
  583. }
  584. }
  585. (*modary)[*nmods] = toadd;
  586. (*nmods)++;
  587. }
  588. /* ------------------------------------------------------------ */
  589. /*
  590. * preop_modify - pre-operation plug-in for modify
  591. */
  592. static int
  593. preop_modify(Slapi_PBlock *pb)
  594. {
  595. int result = LDAP_SUCCESS;
  596. Slapi_PBlock *spb = NULL;
  597. LDAPMod **checkmods = NULL;
  598. int checkmodsCapacity = 0;
  599. #ifdef DEBUG
  600. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  601. "MODIFY begin\n");
  602. #endif
  603. BEGIN
  604. int err;
  605. char *attrName = NULL;
  606. char *markerObjectClass=NULL;
  607. char *requiredObjectClass=NULL;
  608. LDAPMod **mods;
  609. int modcount = 0;
  610. int ii;
  611. LDAPMod *mod;
  612. char *dn;
  613. int isupdatedn;
  614. int argc;
  615. char **argv = NULL;
  616. /*
  617. * If this is a replication update, just be a noop.
  618. */
  619. err = slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &isupdatedn);
  620. if (err) { result = uid_op_error(60); break; }
  621. if (isupdatedn)
  622. {
  623. break;
  624. }
  625. /*
  626. * Get the arguments
  627. */
  628. result = getArguments(pb, &attrName, &markerObjectClass,
  629. &requiredObjectClass);
  630. if (UNTAGGED_PARAMETER == result)
  631. {
  632. result = LDAP_SUCCESS;
  633. /* Statically defined subtrees to monitor */
  634. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc);
  635. if (err) { result = uid_op_error(53); break; }
  636. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv);
  637. if (err) { result = uid_op_error(54); break; }
  638. argc--; /* First argument was attribute name */
  639. argv++;
  640. } else if (0 != result)
  641. {
  642. break;
  643. }
  644. err = slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  645. if (err) { result = uid_op_error(61); break; }
  646. /* There may be more than one mod that matches e.g.
  647. changetype: modify
  648. delete: uid
  649. uid: balster1950
  650. -
  651. add: uid
  652. uid: scottg
  653. So, we need to first find all mods that contain the attribute
  654. which are add or replace ops and are bvalue encoded
  655. */
  656. /* find out how many mods meet this criteria */
  657. for(;*mods;mods++)
  658. {
  659. mod = *mods;
  660. if ((slapi_attr_type_cmp(mod->mod_type, attrName, 1) == 0) && /* mod contains target attr */
  661. (mod->mod_op & LDAP_MOD_BVALUES) && /* mod is bval encoded (not string val) */
  662. (mod->mod_bvalues && mod->mod_bvalues[0]) && /* mod actually contains some values */
  663. (SLAPI_IS_MOD_ADD(mod->mod_op) || /* mod is add */
  664. SLAPI_IS_MOD_REPLACE(mod->mod_op))) /* mod is replace */
  665. {
  666. addMod(&checkmods, &checkmodsCapacity, &modcount, mod);
  667. }
  668. }
  669. if (modcount == 0) {
  670. break; /* no mods to check, we are done */
  671. }
  672. /* Get the target DN */
  673. err = slapi_pblock_get(pb, SLAPI_MODIFY_TARGET, &dn);
  674. if (err) { result = uid_op_error(11); break; }
  675. /*
  676. * Check if it has the required object class
  677. */
  678. if (requiredObjectClass &&
  679. !(spb = dnHasObjectClass(dn, requiredObjectClass))) { break; }
  680. /*
  681. * Passed all the requirements - this is an operation we
  682. * need to enforce uniqueness on. Now find all parent entries
  683. * with the marker object class, and do a search for each one.
  684. */
  685. /*
  686. * stop checking at first mod that fails the check
  687. */
  688. for (ii = 0; (result == 0) && (ii < modcount); ++ii)
  689. {
  690. mod = checkmods[ii];
  691. if (NULL != markerObjectClass)
  692. {
  693. /* Subtree defined by location of marker object class */
  694. result = findSubtreeAndSearch(dn, attrName, NULL, mod->mod_bvalues,
  695. requiredObjectClass, dn, markerObjectClass);
  696. } else
  697. {
  698. /* Subtrees listed on invocation line */
  699. result = searchAllSubtrees(argc, argv, attrName, NULL,
  700. mod->mod_bvalues, requiredObjectClass, dn);
  701. }
  702. }
  703. END
  704. slapi_ch_free((void **)&checkmods);
  705. freePblock(spb);
  706. if (result)
  707. {
  708. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  709. "MODIFY result %d\n", result);
  710. slapi_send_ldap_result(pb, result, 0, moreInfo, 0, 0);
  711. }
  712. return (result==LDAP_SUCCESS)?0:-1;
  713. }
  714. /* ------------------------------------------------------------ */
  715. /*
  716. * preop_modrdn - Pre-operation call for modify RDN
  717. *
  718. * Check that the new RDN does not include attributes that
  719. * cause a constraint violation
  720. */
  721. static int
  722. preop_modrdn(Slapi_PBlock *pb)
  723. {
  724. int result = LDAP_SUCCESS;
  725. Slapi_Entry *e = NULL;
  726. Slapi_DN *sdn = NULL;
  727. Slapi_Value *sv_requiredObjectClass = NULL;
  728. #ifdef DEBUG
  729. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  730. "MODRDN begin\n");
  731. #endif
  732. BEGIN
  733. int err;
  734. char *attrName = NULL;
  735. char *markerObjectClass=NULL;
  736. char *requiredObjectClass=NULL;
  737. char *dn;
  738. char *superior;
  739. char *rdn;
  740. int deloldrdn = 0;
  741. int isupdatedn;
  742. Slapi_Attr *attr;
  743. int argc;
  744. char **argv = NULL;
  745. /*
  746. * If this is a replication update, just be a noop.
  747. */
  748. err = slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &isupdatedn);
  749. if (err) { result = uid_op_error(30); break; }
  750. if (isupdatedn)
  751. {
  752. break;
  753. }
  754. /*
  755. * Get the arguments
  756. */
  757. result = getArguments(pb, &attrName, &markerObjectClass,
  758. &requiredObjectClass);
  759. if (UNTAGGED_PARAMETER == result)
  760. {
  761. result = LDAP_SUCCESS;
  762. /* Statically defined subtrees to monitor */
  763. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc);
  764. if (err) { result = uid_op_error(53); break; }
  765. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv);
  766. if (err) { result = uid_op_error(54); break; }
  767. argc--; /* First argument was attribute name */
  768. argv++;
  769. } else if (0 != result)
  770. {
  771. break;
  772. }
  773. /* Create a Slapi_Value for the requiredObjectClass to use
  774. * for checking the entry. */
  775. if (requiredObjectClass) {
  776. sv_requiredObjectClass = slapi_value_new_string(requiredObjectClass);
  777. }
  778. /* Get the DN of the entry being renamed */
  779. err = slapi_pblock_get(pb, SLAPI_MODRDN_TARGET, &dn);
  780. if (err) { result = uid_op_error(31); break; }
  781. /* Create a Slapi_DN to use for searching. */
  782. sdn = slapi_sdn_new_dn_byref(dn);
  783. /* Get superior value - unimplemented in 3.0/4.0/5.0 DS */
  784. err = slapi_pblock_get(pb, SLAPI_MODRDN_NEWSUPERIOR, &superior);
  785. if (err) { result = uid_op_error(32); break; }
  786. /*
  787. * No superior means the entry is just renamed at
  788. * its current level in the tree. Use the target DN for
  789. * determining which managed tree this belongs to
  790. */
  791. if (!superior) superior = dn;
  792. /* Get the new RDN - this has the attribute values */
  793. err = slapi_pblock_get(pb, SLAPI_MODRDN_NEWRDN, &rdn);
  794. if (err) { result = uid_op_error(33); break; }
  795. #ifdef DEBUG
  796. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  797. "MODRDN newrdn=%s\n", rdn);
  798. #endif
  799. /* See if the old RDN value is being deleted. */
  800. err = slapi_pblock_get(pb, SLAPI_MODRDN_DELOLDRDN, &deloldrdn);
  801. if (err) { result = uid_op_error(34); break; }
  802. /* Get the entry that is being renamed so we can make a dummy copy
  803. * of what it will look like after the rename. */
  804. err = slapi_search_internal_get_entry(sdn, NULL, &e, plugin_identity);
  805. if (err != LDAP_SUCCESS) { result = uid_op_error(35); break; }
  806. /* Apply the rename operation to the dummy entry. */
  807. err = slapi_entry_rename(e, rdn, deloldrdn, superior);
  808. if (err != LDAP_SUCCESS) { result = uid_op_error(36); break; }
  809. /*
  810. * Find any unique attribute data in the new RDN
  811. */
  812. err = slapi_entry_attr_find(e, attrName, &attr);
  813. if (err) break; /* no UID attribute */
  814. /*
  815. * Check if it has the required object class
  816. */
  817. if (requiredObjectClass &&
  818. !slapi_entry_attr_has_syntax_value(e, SLAPI_ATTR_OBJECTCLASS, sv_requiredObjectClass)) { break; }
  819. /*
  820. * Passed all the requirements - this is an operation we
  821. * need to enforce uniqueness on. Now find all parent entries
  822. * with the marker object class, and do a search for each one.
  823. */
  824. if (NULL != markerObjectClass)
  825. {
  826. /* Subtree defined by location of marker object class */
  827. result = findSubtreeAndSearch(slapi_entry_get_dn(e), attrName, attr, NULL,
  828. requiredObjectClass, dn,
  829. markerObjectClass);
  830. } else
  831. {
  832. /* Subtrees listed on invocation line */
  833. result = searchAllSubtrees(argc, argv, attrName, attr, NULL,
  834. requiredObjectClass, dn);
  835. }
  836. END
  837. /* Clean-up */
  838. slapi_sdn_free(&sdn);
  839. slapi_value_free(&sv_requiredObjectClass);
  840. if (e) slapi_entry_free(e);
  841. if (result)
  842. {
  843. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  844. "MODRDN result %d\n", result);
  845. slapi_send_ldap_result(pb, result, 0, moreInfo, 0, 0);
  846. }
  847. return (result==LDAP_SUCCESS)?0:-1;
  848. }
  849. /* ------------------------------------------------------------ */
  850. /*
  851. * Initialize the plugin
  852. *
  853. * uidunique_init (the old name) is deprecated
  854. */
  855. int
  856. NSUniqueAttr_Init(Slapi_PBlock *pb)
  857. {
  858. int err = 0;
  859. BEGIN
  860. int argc;
  861. char **argv;
  862. /* Declare plugin version */
  863. err = slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  864. SLAPI_PLUGIN_VERSION_01);
  865. if (err) break;
  866. /*
  867. * Get plugin identity and store it for later use
  868. * Used for internal operations
  869. */
  870. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &plugin_identity);
  871. /* PR_ASSERT (plugin_identity); */
  872. /*
  873. * Get and normalize arguments
  874. */
  875. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc);
  876. if (err) break;
  877. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv);
  878. if (err) break;
  879. /* First argument is the unique attribute name */
  880. if (argc < 1) { err = -1; break; }
  881. argv++; argc--;
  882. for(;argc > 0;argc--, argv++)
  883. slapi_dn_normalize_case(*argv);
  884. /* Provide descriptive information */
  885. err = slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  886. (void*)&pluginDesc);
  887. if (err) break;
  888. /* Register functions */
  889. err = slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_ADD_FN,
  890. (void*)preop_add);
  891. if (err) break;
  892. err = slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_MODIFY_FN,
  893. (void*)preop_modify);
  894. if (err) break;
  895. err = slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_MODRDN_FN,
  896. (void*)preop_modrdn);
  897. if (err) break;
  898. END
  899. if (err) {
  900. slapi_log_error(SLAPI_LOG_PLUGIN, "NSUniqueAttr_Init",
  901. "Error: %d\n", err);
  902. err = -1;
  903. }
  904. else
  905. slapi_log_error(SLAPI_LOG_PLUGIN, "NSUniqueAttr_Init",
  906. "plugin loaded\n");
  907. return err;
  908. }
  909. int
  910. uidunique_init(Slapi_PBlock *pb)
  911. {
  912. return NSUniqueAttr_Init(pb);
  913. }
  914. /* ------------------------------------------------------------ */
  915. /*
  916. * ldap_quote_filter_value
  917. *
  918. * Quote the filter value according to RFC 2254 (Dec 1997)
  919. *
  920. * value - a UTF8 string containing the value. It may contain
  921. * any of the chars needing quotes ( '(' ')' '*' '/' and NUL ).
  922. * len - the length of the UTF8 value
  923. * out - a buffer to recieve the converted value. May be NULL, in
  924. * which case, only the length of the output is computed (and placed in
  925. * outLen).
  926. * maxLen - the size of the output buffer. It is an error if this length
  927. * is exceeded. Ignored if out is NULL.
  928. * outLen - recieves the size of the output. If an error occurs, this
  929. * result is not available.
  930. *
  931. * Returns
  932. * 0 - success
  933. * -1 - failure (usually a buffer overflow)
  934. */
  935. int /* Error value */
  936. ldap_quote_filter_value(
  937. char *value, int len,
  938. char *out, int maxLen,
  939. int *outLen)
  940. {
  941. int err;
  942. char *eValue;
  943. int resLen;
  944. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  945. static char hexchars[16] = "0123456789abcdef";
  946. #endif
  947. err = 0;
  948. eValue = &value[len];
  949. resLen = 0;
  950. /*
  951. * Convert each character in the input string
  952. */
  953. while(value < eValue)
  954. {
  955. switch(*value)
  956. {
  957. case '(':
  958. case ')':
  959. case '*':
  960. case '\\':
  961. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  962. case 0:
  963. #endif
  964. /* Handle characters needing special escape sequences */
  965. /* Compute size of output */
  966. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  967. resLen += 3;
  968. #else
  969. resLen += 2;
  970. #endif
  971. /* Generate output if requested */
  972. if (out)
  973. {
  974. /* Check for overflow */
  975. if (resLen > maxLen) { err = -1; break; }
  976. *out++ = '\\';
  977. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  978. *out++ = hexchars[(*value >> 4) & 0xF];
  979. *out++ = hexchars[*value & 0xF];
  980. #else
  981. *out++ = *value;
  982. #endif
  983. }
  984. break;
  985. default:
  986. /* Compute size of output */
  987. resLen += 1;
  988. /* Generate output if requested */
  989. if (out)
  990. {
  991. if (resLen > maxLen) { err = -1; break; }
  992. *out++ = *value;
  993. }
  994. break;
  995. }
  996. if (err) break;
  997. value++;
  998. }
  999. if (!err) *outLen = resLen;
  1000. return err;
  1001. }