uid.c 33 KB

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