uid.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  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 (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(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. char *errtext = NULL;
  474. char *attrName = NULL;
  475. #ifdef DEBUG
  476. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name, "ADD begin\n");
  477. #endif
  478. result = LDAP_SUCCESS;
  479. /*
  480. * Do constraint check on the added entry. Set result.
  481. */
  482. BEGIN
  483. int err;
  484. char *markerObjectClass = NULL;
  485. char *requiredObjectClass = NULL;
  486. char *dn;
  487. int isupdatedn;
  488. Slapi_Entry *e;
  489. Slapi_Attr *attr;
  490. int argc;
  491. char **argv = NULL;
  492. /*
  493. * If this is a replication update, just be a noop.
  494. */
  495. err = slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &isupdatedn);
  496. if (err) { result = uid_op_error(50); break; }
  497. if (isupdatedn)
  498. {
  499. break;
  500. }
  501. /*
  502. * Get the arguments
  503. */
  504. result = getArguments(pb, &attrName, &markerObjectClass,
  505. &requiredObjectClass);
  506. if (UNTAGGED_PARAMETER == result)
  507. {
  508. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  509. "ADD parameter untagged: %s\n", attrName);
  510. result = LDAP_SUCCESS;
  511. /* Statically defined subtrees to monitor */
  512. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc);
  513. if (err) { result = uid_op_error(53); break; }
  514. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv);
  515. if (err) { result = uid_op_error(54); break; }
  516. argc--; argv++; /* First argument was attribute name */
  517. } else if (0 != result)
  518. {
  519. break;
  520. }
  521. /*
  522. * Get the target DN for this add operation
  523. */
  524. err = slapi_pblock_get(pb, SLAPI_ADD_TARGET, &dn);
  525. if (err) { result = uid_op_error(51); break; }
  526. #ifdef DEBUG
  527. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name, "ADD target=%s\n", dn);
  528. #endif
  529. /*
  530. * Get the entry data for this add. Check whether it
  531. * contains a value for the unique attribute
  532. */
  533. err = slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e);
  534. if (err) { result = uid_op_error(52); break; }
  535. err = slapi_entry_attr_find(e, attrName, &attr);
  536. if (err) break; /* no unique attribute */
  537. /*
  538. * Check if it contains the required object class
  539. */
  540. if (NULL != requiredObjectClass)
  541. {
  542. if (!entryHasObjectClass(pb, e, requiredObjectClass))
  543. {
  544. /* No, so we don't have to do anything */
  545. break;
  546. }
  547. }
  548. /*
  549. * Passed all the requirements - this is an operation we
  550. * need to enforce uniqueness on. Now find all parent entries
  551. * with the marker object class, and do a search for each one.
  552. */
  553. if (NULL != markerObjectClass)
  554. {
  555. /* Subtree defined by location of marker object class */
  556. result = findSubtreeAndSearch(dn, attrName, attr, NULL,
  557. requiredObjectClass, dn,
  558. markerObjectClass);
  559. } else
  560. {
  561. /* Subtrees listed on invocation line */
  562. result = searchAllSubtrees(argc, argv, attrName, attr, NULL,
  563. requiredObjectClass, dn);
  564. }
  565. END
  566. if (result)
  567. {
  568. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  569. "ADD result %d\n", result);
  570. if (result == LDAP_CONSTRAINT_VIOLATION) {
  571. errtext = slapi_ch_smprintf(moreInfo, attrName);
  572. } else {
  573. errtext = slapi_ch_strdup("Error checking for attribute uniqueness.");
  574. }
  575. /* Send failure to the client */
  576. slapi_send_ldap_result(pb, result, 0, errtext, 0, 0);
  577. slapi_ch_free_string(&errtext);
  578. }
  579. return (result==LDAP_SUCCESS)?0:-1;
  580. }
  581. static void
  582. addMod(LDAPMod ***modary, int *capacity, int *nmods, LDAPMod *toadd)
  583. {
  584. if (*nmods == *capacity) {
  585. *capacity += 4;
  586. if (*modary) {
  587. *modary = (LDAPMod **)slapi_ch_realloc((char *)*modary, *capacity * sizeof(LDAPMod *));
  588. } else {
  589. *modary = (LDAPMod **)slapi_ch_malloc(*capacity * sizeof(LDAPMod *));
  590. }
  591. }
  592. (*modary)[*nmods] = toadd;
  593. (*nmods)++;
  594. }
  595. /* ------------------------------------------------------------ */
  596. /*
  597. * preop_modify - pre-operation plug-in for modify
  598. */
  599. static int
  600. preop_modify(Slapi_PBlock *pb)
  601. {
  602. int result = LDAP_SUCCESS;
  603. Slapi_PBlock *spb = NULL;
  604. LDAPMod **checkmods = NULL;
  605. int checkmodsCapacity = 0;
  606. char *errtext = NULL;
  607. char *attrName = NULL;
  608. #ifdef DEBUG
  609. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  610. "MODIFY begin\n");
  611. #endif
  612. BEGIN
  613. int err;
  614. char *markerObjectClass=NULL;
  615. char *requiredObjectClass=NULL;
  616. LDAPMod **mods;
  617. int modcount = 0;
  618. int ii;
  619. LDAPMod *mod;
  620. char *dn;
  621. int isupdatedn;
  622. int argc;
  623. char **argv = NULL;
  624. /*
  625. * If this is a replication update, just be a noop.
  626. */
  627. err = slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &isupdatedn);
  628. if (err) { result = uid_op_error(60); break; }
  629. if (isupdatedn)
  630. {
  631. break;
  632. }
  633. /*
  634. * Get the arguments
  635. */
  636. result = getArguments(pb, &attrName, &markerObjectClass,
  637. &requiredObjectClass);
  638. if (UNTAGGED_PARAMETER == result)
  639. {
  640. result = LDAP_SUCCESS;
  641. /* Statically defined subtrees to monitor */
  642. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc);
  643. if (err) { result = uid_op_error(53); break; }
  644. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv);
  645. if (err) { result = uid_op_error(54); break; }
  646. argc--; /* First argument was attribute name */
  647. argv++;
  648. } else if (0 != result)
  649. {
  650. break;
  651. }
  652. err = slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  653. if (err) { result = uid_op_error(61); break; }
  654. /* There may be more than one mod that matches e.g.
  655. changetype: modify
  656. delete: uid
  657. uid: balster1950
  658. -
  659. add: uid
  660. uid: scottg
  661. So, we need to first find all mods that contain the attribute
  662. which are add or replace ops and are bvalue encoded
  663. */
  664. /* find out how many mods meet this criteria */
  665. for(;*mods;mods++)
  666. {
  667. mod = *mods;
  668. if ((slapi_attr_type_cmp(mod->mod_type, attrName, 1) == 0) && /* mod contains target attr */
  669. (mod->mod_op & LDAP_MOD_BVALUES) && /* mod is bval encoded (not string val) */
  670. (mod->mod_bvalues && mod->mod_bvalues[0]) && /* mod actually contains some values */
  671. (SLAPI_IS_MOD_ADD(mod->mod_op) || /* mod is add */
  672. SLAPI_IS_MOD_REPLACE(mod->mod_op))) /* mod is replace */
  673. {
  674. addMod(&checkmods, &checkmodsCapacity, &modcount, mod);
  675. }
  676. }
  677. if (modcount == 0) {
  678. break; /* no mods to check, we are done */
  679. }
  680. /* Get the target DN */
  681. err = slapi_pblock_get(pb, SLAPI_MODIFY_TARGET, &dn);
  682. if (err) { result = uid_op_error(11); break; }
  683. /*
  684. * Check if it has the required object class
  685. */
  686. if (requiredObjectClass &&
  687. !(spb = dnHasObjectClass(dn, requiredObjectClass))) { break; }
  688. /*
  689. * Passed all the requirements - this is an operation we
  690. * need to enforce uniqueness on. Now find all parent entries
  691. * with the marker object class, and do a search for each one.
  692. */
  693. /*
  694. * stop checking at first mod that fails the check
  695. */
  696. for (ii = 0; (result == 0) && (ii < modcount); ++ii)
  697. {
  698. mod = checkmods[ii];
  699. if (NULL != markerObjectClass)
  700. {
  701. /* Subtree defined by location of marker object class */
  702. result = findSubtreeAndSearch(dn, attrName, NULL, mod->mod_bvalues,
  703. requiredObjectClass, dn, markerObjectClass);
  704. } else
  705. {
  706. /* Subtrees listed on invocation line */
  707. result = searchAllSubtrees(argc, argv, attrName, NULL,
  708. mod->mod_bvalues, requiredObjectClass, dn);
  709. }
  710. }
  711. END
  712. slapi_ch_free((void **)&checkmods);
  713. freePblock(spb);
  714. if (result)
  715. {
  716. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  717. "MODIFY result %d\n", result);
  718. if (result == LDAP_CONSTRAINT_VIOLATION) {
  719. errtext = slapi_ch_smprintf(moreInfo, attrName);
  720. } else {
  721. errtext = slapi_ch_strdup("Error checking for attribute uniqueness.");
  722. }
  723. slapi_send_ldap_result(pb, result, 0, errtext, 0, 0);
  724. slapi_ch_free_string(&errtext);
  725. }
  726. return (result==LDAP_SUCCESS)?0:-1;
  727. }
  728. /* ------------------------------------------------------------ */
  729. /*
  730. * preop_modrdn - Pre-operation call for modify RDN
  731. *
  732. * Check that the new RDN does not include attributes that
  733. * cause a constraint violation
  734. */
  735. static int
  736. preop_modrdn(Slapi_PBlock *pb)
  737. {
  738. int result = LDAP_SUCCESS;
  739. Slapi_Entry *e = NULL;
  740. Slapi_DN *sdn = NULL;
  741. Slapi_Value *sv_requiredObjectClass = NULL;
  742. char *errtext = NULL;
  743. char *attrName = NULL;
  744. #ifdef DEBUG
  745. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  746. "MODRDN begin\n");
  747. #endif
  748. BEGIN
  749. int err;
  750. char *markerObjectClass=NULL;
  751. char *requiredObjectClass=NULL;
  752. char *dn;
  753. char *superior;
  754. char *rdn;
  755. int deloldrdn = 0;
  756. int isupdatedn;
  757. Slapi_Attr *attr;
  758. int argc;
  759. char **argv = NULL;
  760. /*
  761. * If this is a replication update, just be a noop.
  762. */
  763. err = slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &isupdatedn);
  764. if (err) { result = uid_op_error(30); break; }
  765. if (isupdatedn)
  766. {
  767. break;
  768. }
  769. /*
  770. * Get the arguments
  771. */
  772. result = getArguments(pb, &attrName, &markerObjectClass,
  773. &requiredObjectClass);
  774. if (UNTAGGED_PARAMETER == result)
  775. {
  776. result = LDAP_SUCCESS;
  777. /* Statically defined subtrees to monitor */
  778. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc);
  779. if (err) { result = uid_op_error(53); break; }
  780. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv);
  781. if (err) { result = uid_op_error(54); break; }
  782. argc--; /* First argument was attribute name */
  783. argv++;
  784. } else if (0 != result)
  785. {
  786. break;
  787. }
  788. /* Create a Slapi_Value for the requiredObjectClass to use
  789. * for checking the entry. */
  790. if (requiredObjectClass) {
  791. sv_requiredObjectClass = slapi_value_new_string(requiredObjectClass);
  792. }
  793. /* Get the DN of the entry being renamed */
  794. err = slapi_pblock_get(pb, SLAPI_MODRDN_TARGET, &dn);
  795. if (err) { result = uid_op_error(31); break; }
  796. /* Create a Slapi_DN to use for searching. */
  797. sdn = slapi_sdn_new_dn_byref(dn);
  798. /* Get superior value - unimplemented in 3.0/4.0/5.0 DS */
  799. err = slapi_pblock_get(pb, SLAPI_MODRDN_NEWSUPERIOR, &superior);
  800. if (err) { result = uid_op_error(32); break; }
  801. /*
  802. * No superior means the entry is just renamed at
  803. * its current level in the tree. Use the target DN for
  804. * determining which managed tree this belongs to
  805. */
  806. if (!superior) superior = dn;
  807. /* Get the new RDN - this has the attribute values */
  808. err = slapi_pblock_get(pb, SLAPI_MODRDN_NEWRDN, &rdn);
  809. if (err) { result = uid_op_error(33); break; }
  810. #ifdef DEBUG
  811. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  812. "MODRDN newrdn=%s\n", rdn);
  813. #endif
  814. /* See if the old RDN value is being deleted. */
  815. err = slapi_pblock_get(pb, SLAPI_MODRDN_DELOLDRDN, &deloldrdn);
  816. if (err) { result = uid_op_error(34); break; }
  817. /* Get the entry that is being renamed so we can make a dummy copy
  818. * of what it will look like after the rename. */
  819. err = slapi_search_internal_get_entry(sdn, NULL, &e, plugin_identity);
  820. if (err != LDAP_SUCCESS) {
  821. result = uid_op_error(35);
  822. /* We want to return a no such object error if the target doesn't exist. */
  823. if (err == LDAP_NO_SUCH_OBJECT) {
  824. result = err;
  825. }
  826. break;
  827. }
  828. /* Apply the rename operation to the dummy entry. */
  829. err = slapi_entry_rename(e, rdn, deloldrdn, superior);
  830. if (err != LDAP_SUCCESS) { result = uid_op_error(36); break; }
  831. /*
  832. * Find any unique attribute data in the new RDN
  833. */
  834. err = slapi_entry_attr_find(e, attrName, &attr);
  835. if (err) break; /* no UID attribute */
  836. /*
  837. * Check if it has the required object class
  838. */
  839. if (requiredObjectClass &&
  840. !slapi_entry_attr_has_syntax_value(e, SLAPI_ATTR_OBJECTCLASS, sv_requiredObjectClass)) { break; }
  841. /*
  842. * Passed all the requirements - this is an operation we
  843. * need to enforce uniqueness on. Now find all parent entries
  844. * with the marker object class, and do a search for each one.
  845. */
  846. if (NULL != markerObjectClass)
  847. {
  848. /* Subtree defined by location of marker object class */
  849. result = findSubtreeAndSearch(slapi_entry_get_dn(e), attrName, attr, NULL,
  850. requiredObjectClass, dn,
  851. markerObjectClass);
  852. } else
  853. {
  854. /* Subtrees listed on invocation line */
  855. result = searchAllSubtrees(argc, argv, attrName, attr, NULL,
  856. requiredObjectClass, dn);
  857. }
  858. END
  859. /* Clean-up */
  860. slapi_sdn_free(&sdn);
  861. slapi_value_free(&sv_requiredObjectClass);
  862. if (e) slapi_entry_free(e);
  863. if (result)
  864. {
  865. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  866. "MODRDN result %d\n", result);
  867. if (result == LDAP_CONSTRAINT_VIOLATION) {
  868. errtext = slapi_ch_smprintf(moreInfo, attrName);
  869. } else {
  870. errtext = slapi_ch_strdup("Error checking for attribute uniqueness.");
  871. }
  872. slapi_send_ldap_result(pb, result, 0, errtext, 0, 0);
  873. slapi_ch_free_string(&errtext);
  874. }
  875. return (result==LDAP_SUCCESS)?0:-1;
  876. }
  877. /* ------------------------------------------------------------ */
  878. /*
  879. * Initialize the plugin
  880. *
  881. * uidunique_init (the old name) is deprecated
  882. */
  883. int
  884. NSUniqueAttr_Init(Slapi_PBlock *pb)
  885. {
  886. int err = 0;
  887. BEGIN
  888. int argc;
  889. char **argv;
  890. /* Declare plugin version */
  891. err = slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  892. SLAPI_PLUGIN_VERSION_01);
  893. if (err) break;
  894. /*
  895. * Get plugin identity and store it for later use
  896. * Used for internal operations
  897. */
  898. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &plugin_identity);
  899. /* PR_ASSERT (plugin_identity); */
  900. /*
  901. * Get and normalize arguments
  902. */
  903. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc);
  904. if (err) break;
  905. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv);
  906. if (err) break;
  907. /* First argument is the unique attribute name */
  908. if (argc < 1) { err = -1; break; }
  909. argv++; argc--;
  910. for(;argc > 0;argc--, argv++)
  911. slapi_dn_normalize_case(*argv);
  912. /* Provide descriptive information */
  913. err = slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  914. (void*)&pluginDesc);
  915. if (err) break;
  916. /* Register functions */
  917. err = slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_ADD_FN,
  918. (void*)preop_add);
  919. if (err) break;
  920. err = slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_MODIFY_FN,
  921. (void*)preop_modify);
  922. if (err) break;
  923. err = slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_MODRDN_FN,
  924. (void*)preop_modrdn);
  925. if (err) break;
  926. END
  927. if (err) {
  928. slapi_log_error(SLAPI_LOG_PLUGIN, "NSUniqueAttr_Init",
  929. "Error: %d\n", err);
  930. err = -1;
  931. }
  932. else
  933. slapi_log_error(SLAPI_LOG_PLUGIN, "NSUniqueAttr_Init",
  934. "plugin loaded\n");
  935. return err;
  936. }
  937. int
  938. uidunique_init(Slapi_PBlock *pb)
  939. {
  940. return NSUniqueAttr_Init(pb);
  941. }
  942. /* ------------------------------------------------------------ */
  943. /*
  944. * ldap_quote_filter_value
  945. *
  946. * Quote the filter value according to RFC 2254 (Dec 1997)
  947. *
  948. * value - a UTF8 string containing the value. It may contain
  949. * any of the chars needing quotes ( '(' ')' '*' '/' and NUL ).
  950. * len - the length of the UTF8 value
  951. * out - a buffer to recieve the converted value. May be NULL, in
  952. * which case, only the length of the output is computed (and placed in
  953. * outLen).
  954. * maxLen - the size of the output buffer. It is an error if this length
  955. * is exceeded. Ignored if out is NULL.
  956. * outLen - recieves the size of the output. If an error occurs, this
  957. * result is not available.
  958. *
  959. * Returns
  960. * 0 - success
  961. * -1 - failure (usually a buffer overflow)
  962. */
  963. int /* Error value */
  964. ldap_quote_filter_value(
  965. char *value, int len,
  966. char *out, int maxLen,
  967. int *outLen)
  968. {
  969. int err;
  970. char *eValue;
  971. int resLen;
  972. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  973. static char hexchars[16] = "0123456789abcdef";
  974. #endif
  975. err = 0;
  976. eValue = &value[len];
  977. resLen = 0;
  978. /*
  979. * Convert each character in the input string
  980. */
  981. while(value < eValue)
  982. {
  983. switch(*value)
  984. {
  985. case '(':
  986. case ')':
  987. case '*':
  988. case '\\':
  989. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  990. case 0:
  991. #endif
  992. /* Handle characters needing special escape sequences */
  993. /* Compute size of output */
  994. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  995. resLen += 3;
  996. #else
  997. resLen += 2;
  998. #endif
  999. /* Generate output if requested */
  1000. if (out)
  1001. {
  1002. /* Check for overflow */
  1003. if (resLen > maxLen) { err = -1; break; }
  1004. *out++ = '\\';
  1005. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  1006. *out++ = hexchars[(*value >> 4) & 0xF];
  1007. *out++ = hexchars[*value & 0xF];
  1008. #else
  1009. *out++ = *value;
  1010. #endif
  1011. }
  1012. break;
  1013. default:
  1014. /* Compute size of output */
  1015. resLen += 1;
  1016. /* Generate output if requested */
  1017. if (out)
  1018. {
  1019. if (resLen > maxLen) { err = -1; break; }
  1020. *out++ = *value;
  1021. }
  1022. break;
  1023. }
  1024. if (err) break;
  1025. value++;
  1026. }
  1027. if (!err) *outLen = resLen;
  1028. return err;
  1029. }