1
0

uid.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  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. if (requiredObjectClass &&
  676. !(spb = dnHasObjectClass(dn, requiredObjectClass))) { break; }
  677. /*
  678. * Passed all the requirements - this is an operation we
  679. * need to enforce uniqueness on. Now find all parent entries
  680. * with the marker object class, and do a search for each one.
  681. */
  682. /*
  683. * stop checking at first mod that fails the check
  684. */
  685. for (ii = 0; (result == 0) && (ii < modcount); ++ii)
  686. {
  687. mod = checkmods[ii];
  688. if (NULL != markerObjectClass)
  689. {
  690. /* Subtree defined by location of marker object class */
  691. result = findSubtreeAndSearch(dn, attrName, NULL, mod->mod_bvalues,
  692. requiredObjectClass, dn, markerObjectClass);
  693. } else
  694. {
  695. /* Subtrees listed on invocation line */
  696. result = searchAllSubtrees(argc, argv, attrName, NULL,
  697. mod->mod_bvalues, requiredObjectClass, dn);
  698. }
  699. }
  700. END
  701. slapi_ch_free((void **)&checkmods);
  702. freePblock(spb);
  703. if (result)
  704. {
  705. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  706. "MODIFY result %d\n", result);
  707. slapi_send_ldap_result(pb, result, 0, moreInfo, 0, 0);
  708. }
  709. return (result==LDAP_SUCCESS)?0:-1;
  710. }
  711. /* ------------------------------------------------------------ */
  712. /*
  713. * preop_modrdn - Pre-operation call for modify RDN
  714. *
  715. * Check that the new RDN does not include attributes that
  716. * cause a constraint violation
  717. */
  718. static int
  719. preop_modrdn(Slapi_PBlock *pb)
  720. {
  721. int result;
  722. Slapi_Entry *e;
  723. #ifdef DEBUG
  724. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  725. "MODRDN begin\n");
  726. #endif
  727. /* Init */
  728. result = LDAP_SUCCESS;
  729. e = 0;
  730. BEGIN
  731. int err;
  732. char *attrName = NULL;
  733. char *markerObjectClass=NULL;
  734. char *requiredObjectClass=NULL;
  735. char *dn;
  736. char *superior;
  737. char *rdn;
  738. int isupdatedn;
  739. Slapi_Attr *attr;
  740. int argc;
  741. char **argv = NULL;
  742. /*
  743. * If this is a replication update, just be a noop.
  744. */
  745. err = slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &isupdatedn);
  746. if (err) { result = uid_op_error(30); break; }
  747. if (isupdatedn)
  748. {
  749. break;
  750. }
  751. /*
  752. * Get the arguments
  753. */
  754. result = getArguments(pb, &attrName, &markerObjectClass,
  755. &requiredObjectClass);
  756. if (UNTAGGED_PARAMETER == result)
  757. {
  758. result = LDAP_SUCCESS;
  759. /* Statically defined subtrees to monitor */
  760. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc);
  761. if (err) { result = uid_op_error(53); break; }
  762. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv);
  763. if (err) { result = uid_op_error(54); break; }
  764. argc--; /* First argument was attribute name */
  765. argv++;
  766. } else if (0 != result)
  767. {
  768. break;
  769. }
  770. /* Get the DN of the entry being renamed */
  771. err = slapi_pblock_get(pb, SLAPI_MODRDN_TARGET, &dn);
  772. if (err) { result = uid_op_error(31); break; }
  773. /* Get superior value - unimplemented in 3.0/4.0/5.0 DS */
  774. err = slapi_pblock_get(pb, SLAPI_MODRDN_NEWSUPERIOR, &superior);
  775. if (err) { result = uid_op_error(32); break; }
  776. /*
  777. * No superior means the entry is just renamed at
  778. * its current level in the tree. Use the target DN for
  779. * determining which managed tree this belongs to
  780. */
  781. if (!superior) superior = dn;
  782. /* Get the new RDN - this has the attribute values */
  783. err = slapi_pblock_get(pb, SLAPI_MODRDN_NEWRDN, &rdn);
  784. if (err) { result = uid_op_error(33); break; }
  785. #ifdef DEBUG
  786. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  787. "MODRDN newrdn=%s\n", rdn);
  788. #endif
  789. /*
  790. * Parse the RDN into attributes by creating a "dummy" entry
  791. * and setting the attributes from the RDN.
  792. *
  793. * The new entry must be freed.
  794. */
  795. e = slapi_entry_alloc();
  796. if (!e) { result = uid_op_error(34); break; }
  797. /* NOTE: strdup on the rdn, since it will be freed when
  798. * the entry is freed */
  799. slapi_entry_set_dn(e, slapi_ch_strdup(rdn));
  800. err = slapi_entry_add_rdn_values(e);
  801. if (err)
  802. {
  803. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  804. "MODRDN bad rdn value=%s\n", rdn);
  805. break; /* Bad DN */
  806. }
  807. /*
  808. * Find any unique attribute data in the new RDN
  809. */
  810. err = slapi_entry_attr_find(e, attrName, &attr);
  811. if (err) break; /* no UID attribute */
  812. /*
  813. * Passed all the requirements - this is an operation we
  814. * need to enforce uniqueness on. Now find all parent entries
  815. * with the marker object class, and do a search for each one.
  816. */
  817. if (NULL != markerObjectClass)
  818. {
  819. /* Subtree defined by location of marker object class */
  820. result = findSubtreeAndSearch(dn, attrName, attr, NULL,
  821. requiredObjectClass, dn,
  822. markerObjectClass);
  823. } else
  824. {
  825. /* Subtrees listed on invocation line */
  826. result = searchAllSubtrees(argc, argv, attrName, attr, NULL,
  827. requiredObjectClass, dn);
  828. }
  829. END
  830. /* Clean-up */
  831. if (e) slapi_entry_free(e);
  832. if (result)
  833. {
  834. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  835. "MODRDN result %d\n", result);
  836. slapi_send_ldap_result(pb, result, 0, moreInfo, 0, 0);
  837. }
  838. return (result==LDAP_SUCCESS)?0:-1;
  839. }
  840. /* ------------------------------------------------------------ */
  841. /*
  842. * Initialize the plugin
  843. *
  844. * uidunique_init (the old name) is deprecated
  845. */
  846. int
  847. NSUniqueAttr_Init(Slapi_PBlock *pb)
  848. {
  849. int err = 0;
  850. BEGIN
  851. int argc;
  852. char **argv;
  853. /* Declare plugin version */
  854. err = slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  855. SLAPI_PLUGIN_VERSION_01);
  856. if (err) break;
  857. /*
  858. * Get plugin identity and store it for later use
  859. * Used for internal operations
  860. */
  861. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &plugin_identity);
  862. /* PR_ASSERT (plugin_identity); */
  863. /*
  864. * Get and normalize arguments
  865. */
  866. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc);
  867. if (err) break;
  868. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv);
  869. if (err) break;
  870. /* First argument is the unique attribute name */
  871. if (argc < 1) { err = -1; break; }
  872. argv++; argc--;
  873. for(;argc > 0;argc--, argv++)
  874. slapi_dn_normalize_case(*argv);
  875. /* Provide descriptive information */
  876. err = slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  877. (void*)&pluginDesc);
  878. if (err) break;
  879. /* Register functions */
  880. err = slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_ADD_FN,
  881. (void*)preop_add);
  882. if (err) break;
  883. err = slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_MODIFY_FN,
  884. (void*)preop_modify);
  885. if (err) break;
  886. err = slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_MODRDN_FN,
  887. (void*)preop_modrdn);
  888. if (err) break;
  889. END
  890. if (err) {
  891. slapi_log_error(SLAPI_LOG_PLUGIN, "NSUniqueAttr_Init",
  892. "Error: %d\n", err);
  893. err = -1;
  894. }
  895. else
  896. slapi_log_error(SLAPI_LOG_PLUGIN, "NSUniqueAttr_Init",
  897. "plugin loaded\n");
  898. return err;
  899. }
  900. int
  901. uidunique_init(Slapi_PBlock *pb)
  902. {
  903. return NSUniqueAttr_Init(pb);
  904. }
  905. /* ------------------------------------------------------------ */
  906. /*
  907. * ldap_quote_filter_value
  908. *
  909. * Quote the filter value according to RFC 2254 (Dec 1997)
  910. *
  911. * value - a UTF8 string containing the value. It may contain
  912. * any of the chars needing quotes ( '(' ')' '*' '/' and NUL ).
  913. * len - the length of the UTF8 value
  914. * out - a buffer to recieve the converted value. May be NULL, in
  915. * which case, only the length of the output is computed (and placed in
  916. * outLen).
  917. * maxLen - the size of the output buffer. It is an error if this length
  918. * is exceeded. Ignored if out is NULL.
  919. * outLen - recieves the size of the output. If an error occurs, this
  920. * result is not available.
  921. *
  922. * Returns
  923. * 0 - success
  924. * -1 - failure (usually a buffer overflow)
  925. */
  926. int /* Error value */
  927. ldap_quote_filter_value(
  928. char *value, int len,
  929. char *out, int maxLen,
  930. int *outLen)
  931. {
  932. int err;
  933. char *eValue;
  934. int resLen;
  935. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  936. static char hexchars[16] = "0123456789abcdef";
  937. #endif
  938. err = 0;
  939. eValue = &value[len];
  940. resLen = 0;
  941. /*
  942. * Convert each character in the input string
  943. */
  944. while(value < eValue)
  945. {
  946. switch(*value)
  947. {
  948. case '(':
  949. case ')':
  950. case '*':
  951. case '\\':
  952. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  953. case 0:
  954. #endif
  955. /* Handle characters needing special escape sequences */
  956. /* Compute size of output */
  957. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  958. resLen += 3;
  959. #else
  960. resLen += 2;
  961. #endif
  962. /* Generate output if requested */
  963. if (out)
  964. {
  965. /* Check for overflow */
  966. if (resLen > maxLen) { err = -1; break; }
  967. *out++ = '\\';
  968. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  969. *out++ = hexchars[(*value >> 4) & 0xF];
  970. *out++ = hexchars[*value & 0xF];
  971. #else
  972. *out++ = *value;
  973. #endif
  974. }
  975. break;
  976. default:
  977. /* Compute size of output */
  978. resLen += 1;
  979. /* Generate output if requested */
  980. if (out)
  981. {
  982. if (resLen > maxLen) { err = -1; break; }
  983. *out++ = *value;
  984. }
  985. break;
  986. }
  987. if (err) break;
  988. value++;
  989. }
  990. if (!err) *outLen = resLen;
  991. return err;
  992. }