uid.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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 *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)
  128. {
  129. char *filter;
  130. char *fp;
  131. char *max;
  132. int attrLen;
  133. int valueLen;
  134. int filterLen;
  135. PR_ASSERT(attribute);
  136. /* Compute the length of the required buffer */
  137. attrLen = strlen(attribute);
  138. if (ldap_quote_filter_value(value->bv_val,
  139. value->bv_len, 0, 0, &valueLen))
  140. return 0;
  141. filterLen = attrLen + 1 + valueLen + 1;
  142. /* Allocate the buffer */
  143. filter = slapi_ch_malloc(filterLen);
  144. fp = filter;
  145. max = &filter[filterLen];
  146. /* Place attribute name in filter */
  147. strcpy(fp, attribute);
  148. fp += attrLen;
  149. /* Place comparison operator */
  150. *fp++ = '=';
  151. /* Place value in filter */
  152. if (ldap_quote_filter_value(value->bv_val, value->bv_len,
  153. fp, max-fp, &valueLen)) { slapi_ch_free((void**)&filter); return 0; }
  154. fp += valueLen;
  155. /* Terminate */
  156. *fp = 0;
  157. return filter;
  158. }
  159. /* ------------------------------------------------------------ */
  160. /*
  161. * search - search a subtree for entries with a named attribute matching
  162. * the list of values. An entry matching the 'target' DN is
  163. * not considered in the search.
  164. *
  165. * If 'attr' is NULL, the values are taken from 'values'.
  166. * If 'attr' is non-NULL, the values are taken from 'attr'.
  167. *
  168. * Return:
  169. * LDAP_SUCCESS - no matches, or the attribute matches the
  170. * target dn.
  171. * LDAP_CONSTRAINT_VIOLATION - an entry was found that already
  172. * contains the attribute value.
  173. * LDAP_OPERATIONS_ERROR - a server failure.
  174. */
  175. static int
  176. search(const char *baseDN, const char *attrName, Slapi_Attr *attr,
  177. struct berval **values, const char *target)
  178. {
  179. int result;
  180. #ifdef DEBUG
  181. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  182. "SEARCH baseDN=%s attr=%s target=%s\n", baseDN, attrName,
  183. target?target:"None");
  184. #endif
  185. result = LDAP_SUCCESS;
  186. /* If no values, can't possibly be a conflict */
  187. if ( (Slapi_Attr *)NULL == attr && (struct berval **)NULL == values )
  188. return result;
  189. /*
  190. * Perform the search for each value provided
  191. *
  192. * Another possibility would be to search for all the values at once.
  193. * However, this is more complex (for filter creation) and unique
  194. * attributes values are probably only changed one at a time anyway.
  195. */
  196. if ( (Slapi_Attr *)NULL != attr )
  197. {
  198. Slapi_Value *v = NULL;
  199. int vhint = -1;
  200. for ( vhint = slapi_attr_first_value( attr, &v );
  201. vhint != -1 && LDAP_SUCCESS == result;
  202. vhint = slapi_attr_next_value( attr, vhint, &v ))
  203. {
  204. result = search_one_berval(baseDN,attrName,
  205. slapi_value_get_berval(v),target);
  206. }
  207. }
  208. else
  209. {
  210. for (;*values != NULL && LDAP_SUCCESS == result; values++)
  211. {
  212. result = search_one_berval(baseDN,attrName,*values,target);
  213. }
  214. }
  215. #ifdef DEBUG
  216. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  217. "SEARCH result = %d\n", result);
  218. #endif
  219. return( result );
  220. }
  221. static int
  222. search_one_berval(const char *baseDN, const char *attrName,
  223. const struct berval *value, const char *target)
  224. {
  225. int result;
  226. char *filter;
  227. Slapi_PBlock *spb;
  228. result = LDAP_SUCCESS;
  229. /* If no value, can't possibly be a conflict */
  230. if ( (struct berval *)NULL == value )
  231. return result;
  232. filter = 0;
  233. spb = 0;
  234. BEGIN
  235. int err;
  236. int sres;
  237. Slapi_Entry **entries;
  238. static char *attrs[] = { "1.1", 0 };
  239. /* Create the filter - this needs to be freed */
  240. filter = create_filter(attrName, value);
  241. #ifdef DEBUG
  242. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  243. "SEARCH filter=%s\n", filter);
  244. #endif
  245. /* Perform the search using the new internal API */
  246. spb = slapi_pblock_new();
  247. if (!spb) { result = uid_op_error(2); break; }
  248. slapi_search_internal_set_pb(spb, baseDN, LDAP_SCOPE_SUBTREE,
  249. filter, attrs, 0 /* attrs only */, NULL, NULL, plugin_identity, 0 /* actions */);
  250. slapi_search_internal_pb(spb);
  251. err = slapi_pblock_get(spb, SLAPI_PLUGIN_INTOP_RESULT, &sres);
  252. if (err) { result = uid_op_error(3); break; }
  253. /* Allow search to report that there is nothing in the subtree */
  254. if (sres == LDAP_NO_SUCH_OBJECT) break;
  255. /* Other errors are bad */
  256. if (sres) { result = uid_op_error(3); break; }
  257. err = slapi_pblock_get(spb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  258. &entries);
  259. if (err) { result = uid_op_error(4); break; }
  260. /*
  261. * Look at entries returned. Any entry found must be the
  262. * target entry or the constraint fails.
  263. */
  264. for(;*entries;entries++)
  265. {
  266. char *dn = slapi_entry_get_dn(*entries);
  267. /*
  268. * DNs are returned in the original value used to insert
  269. * the entry. This must be "normalized" for comparison.
  270. *
  271. * This normalization is done "in-place" (modifying the value
  272. * in the entry). This is OK, since this is the only user
  273. * of this copy of the entry.
  274. */
  275. slapi_dn_normalize_case(dn);
  276. #ifdef DEBUG
  277. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  278. "SEARCH entry dn=%s\n", dn);
  279. #endif
  280. /*
  281. * It is a Constraint Violation if any entry is found, unless
  282. * the entry is the target entry (if any).
  283. */
  284. if (!target || strcmp(dn, target) != 0)
  285. {
  286. result = LDAP_CONSTRAINT_VIOLATION;
  287. break;
  288. }
  289. }
  290. #ifdef DEBUG
  291. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  292. "SEARCH complete result=%d\n", result);
  293. #endif
  294. END
  295. /* Clean-up */
  296. if (spb) {
  297. slapi_free_search_results_internal(spb);
  298. slapi_pblock_destroy(spb);
  299. }
  300. slapi_ch_free((void**)&filter);
  301. return result;
  302. }
  303. /* ------------------------------------------------------------ */
  304. /*
  305. * searchAllSubtrees - search all subtrees in argv for entries
  306. * with a named attribute matching the list of values, by
  307. * calling search for each one.
  308. *
  309. * If 'attr' is NULL, the values are taken from 'values'.
  310. * If 'attr' is non-NULL, the values are taken from 'attr'.
  311. *
  312. * Return:
  313. * LDAP_SUCCESS - no matches, or the attribute matches the
  314. * target dn.
  315. * LDAP_CONSTRAINT_VIOLATION - an entry was found that already
  316. * contains the attribute value.
  317. * LDAP_OPERATIONS_ERROR - a server failure.
  318. */
  319. static int
  320. searchAllSubtrees(int argc, char *argv[], const char *attrName,
  321. Slapi_Attr *attr, struct berval **values, const char *dn)
  322. {
  323. int result = LDAP_SUCCESS;
  324. /*
  325. * For each DN in the managed list, do uniqueness checking if
  326. * the target DN is a subnode in the tree.
  327. */
  328. for(;argc > 0;argc--,argv++)
  329. {
  330. /*
  331. * The DN should already be normalized, so we don't have to
  332. * worry about that here.
  333. */
  334. if (slapi_dn_issuffix(dn, *argv)) {
  335. result = search(*argv, attrName, attr, values, dn);
  336. if (result) break;
  337. }
  338. }
  339. return result;
  340. }
  341. /* ------------------------------------------------------------ */
  342. /*
  343. * getArguments - parse invocation parameters
  344. * Return:
  345. * 0 - success
  346. * >0 - error parsing parameters
  347. */
  348. static int
  349. getArguments(Slapi_PBlock *pb, char **attrName, char **markerObjectClass,
  350. char **requiredObjectClass)
  351. {
  352. int argc;
  353. char **argv;
  354. /*
  355. * Get the arguments
  356. */
  357. if (slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc))
  358. {
  359. return uid_op_error(10);
  360. }
  361. if (slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv))
  362. {
  363. return uid_op_error(11);
  364. }
  365. /*
  366. * Required arguments: attribute and markerObjectClass
  367. * Optional argument: requiredObjectClass
  368. */
  369. for(;argc > 0;argc--,argv++)
  370. {
  371. char *param = *argv;
  372. char *delimiter = strchr(param, '=');
  373. if (NULL == delimiter)
  374. {
  375. /* Old style untagged parameter */
  376. *attrName = *argv;
  377. return UNTAGGED_PARAMETER;
  378. }
  379. if (strncasecmp(param, "attribute", delimiter-param) == 0)
  380. {
  381. /* It's OK to set a pointer here, because ultimately it points
  382. * inside the argv array of the pblock, which will be staying
  383. * arround.
  384. */
  385. *attrName = delimiter+1;
  386. } else if (strncasecmp(param, "markerobjectclass", delimiter-param) == 0)
  387. {
  388. *markerObjectClass = delimiter+1;
  389. } else if (strncasecmp(param, "requiredobjectclass", delimiter-param) == 0)
  390. {
  391. *requiredObjectClass = delimiter+1;
  392. }
  393. }
  394. if (!*attrName || !*markerObjectClass)
  395. {
  396. return uid_op_error(13);
  397. }
  398. return 0;
  399. }
  400. /* ------------------------------------------------------------ */
  401. /*
  402. * findSubtreeAndSearch - walk up the tree to find an entry with
  403. * the marker object class; if found, call search from there and
  404. * return the result it returns
  405. *
  406. * If 'attr' is NULL, the values are taken from 'values'.
  407. * If 'attr' is non-NULL, the values are taken from 'attr'.
  408. *
  409. * Return:
  410. * LDAP_SUCCESS - no matches, or the attribute matches the
  411. * target dn.
  412. * LDAP_CONSTRAINT_VIOLATION - an entry was found that already
  413. * contains the attribute value.
  414. * LDAP_OPERATIONS_ERROR - a server failure.
  415. */
  416. static int
  417. findSubtreeAndSearch(char *parentDN, const char *attrName, Slapi_Attr *attr,
  418. struct berval **values, const char *target, const char *markerObjectClass)
  419. {
  420. int result = LDAP_SUCCESS;
  421. Slapi_PBlock *spb = NULL;
  422. while (NULL != (parentDN = slapi_dn_parent(parentDN)))
  423. {
  424. if ((spb = dnHasObjectClass(parentDN, markerObjectClass)))
  425. {
  426. freePblock(spb);
  427. /*
  428. * Do the search. There is no entry that is allowed
  429. * to have the attribute already.
  430. */
  431. result = search(parentDN, attrName, attr, values, target);
  432. break;
  433. }
  434. }
  435. return result;
  436. }
  437. /* ------------------------------------------------------------ */
  438. /*
  439. * preop_add - pre-operation plug-in for add
  440. */
  441. static int
  442. preop_add(Slapi_PBlock *pb)
  443. {
  444. int result;
  445. #ifdef DEBUG
  446. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name, "ADD begin\n");
  447. #endif
  448. result = LDAP_SUCCESS;
  449. /*
  450. * Do constraint check on the added entry. Set result.
  451. */
  452. BEGIN
  453. int err;
  454. char *attrName = NULL;
  455. char *markerObjectClass = NULL;
  456. char *requiredObjectClass = NULL;
  457. char *dn;
  458. int isupdatedn;
  459. Slapi_Entry *e;
  460. Slapi_Attr *attr;
  461. int argc;
  462. char **argv = NULL;
  463. /*
  464. * If this is a replication update, just be a noop.
  465. */
  466. err = slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &isupdatedn);
  467. if (err) { result = uid_op_error(50); break; }
  468. if (isupdatedn)
  469. {
  470. break;
  471. }
  472. /*
  473. * Get the arguments
  474. */
  475. result = getArguments(pb, &attrName, &markerObjectClass,
  476. &requiredObjectClass);
  477. if (UNTAGGED_PARAMETER == result)
  478. {
  479. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  480. "ADD parameter untagged: %s\n", attrName);
  481. result = LDAP_SUCCESS;
  482. /* Statically defined subtrees to monitor */
  483. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc);
  484. if (err) { result = uid_op_error(53); break; }
  485. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv);
  486. if (err) { result = uid_op_error(54); break; }
  487. argc--; argv++; /* First argument was attribute name */
  488. } else if (0 != result)
  489. {
  490. break;
  491. }
  492. /*
  493. * Get the target DN for this add operation
  494. */
  495. err = slapi_pblock_get(pb, SLAPI_ADD_TARGET, &dn);
  496. if (err) { result = uid_op_error(51); break; }
  497. #ifdef DEBUG
  498. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name, "ADD target=%s\n", dn);
  499. #endif
  500. /*
  501. * Get the entry data for this add. Check whether it
  502. * contains a value for the unique attribute
  503. */
  504. err = slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e);
  505. if (err) { result = uid_op_error(52); break; }
  506. err = slapi_entry_attr_find(e, attrName, &attr);
  507. if (err) break; /* no unique attribute */
  508. /*
  509. * Check if it contains the required object class
  510. */
  511. if (NULL != requiredObjectClass)
  512. {
  513. if (!entryHasObjectClass(pb, e, requiredObjectClass))
  514. {
  515. /* No, so we don't have to do anything */
  516. break;
  517. }
  518. }
  519. /*
  520. * Passed all the requirements - this is an operation we
  521. * need to enforce uniqueness on. Now find all parent entries
  522. * with the marker object class, and do a search for each one.
  523. */
  524. if (NULL != markerObjectClass)
  525. {
  526. /* Subtree defined by location of marker object class */
  527. result = findSubtreeAndSearch(dn, attrName, attr, NULL,
  528. dn, markerObjectClass);
  529. } else
  530. {
  531. /* Subtrees listed on invocation line */
  532. result = searchAllSubtrees(argc, argv, attrName, attr, NULL, dn);
  533. }
  534. END
  535. if (result)
  536. {
  537. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  538. "ADD result %d\n", result);
  539. /* Send failure to the client */
  540. slapi_send_ldap_result(pb, result, 0, moreInfo, 0, 0);
  541. }
  542. return (result==LDAP_SUCCESS)?0:-1;
  543. }
  544. static void
  545. addMod(LDAPMod ***modary, int *capacity, int *nmods, LDAPMod *toadd)
  546. {
  547. if (*nmods == *capacity) {
  548. *capacity += 4;
  549. if (*modary) {
  550. *modary = (LDAPMod **)slapi_ch_realloc((char *)*modary, *capacity * sizeof(LDAPMod *));
  551. } else {
  552. *modary = (LDAPMod **)slapi_ch_malloc(*capacity * sizeof(LDAPMod *));
  553. }
  554. }
  555. *modary[*nmods] = toadd;
  556. (*nmods)++;
  557. }
  558. /* ------------------------------------------------------------ */
  559. /*
  560. * preop_modify - pre-operation plug-in for modify
  561. */
  562. static int
  563. preop_modify(Slapi_PBlock *pb)
  564. {
  565. int result = LDAP_SUCCESS;
  566. Slapi_PBlock *spb = NULL;
  567. LDAPMod **checkmods = NULL;
  568. int checkmodsCapacity = 0;
  569. #ifdef DEBUG
  570. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  571. "MODIFY begin\n");
  572. #endif
  573. BEGIN
  574. int err;
  575. char *attrName = NULL;
  576. char *markerObjectClass=NULL;
  577. char *requiredObjectClass=NULL;
  578. LDAPMod **mods;
  579. int modcount = 0;
  580. int ii;
  581. LDAPMod *mod;
  582. char *dn;
  583. int isupdatedn;
  584. int argc;
  585. char **argv = NULL;
  586. /*
  587. * If this is a replication update, just be a noop.
  588. */
  589. err = slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &isupdatedn);
  590. if (err) { result = uid_op_error(60); break; }
  591. if (isupdatedn)
  592. {
  593. break;
  594. }
  595. /*
  596. * Get the arguments
  597. */
  598. result = getArguments(pb, &attrName, &markerObjectClass,
  599. &requiredObjectClass);
  600. if (UNTAGGED_PARAMETER == result)
  601. {
  602. result = LDAP_SUCCESS;
  603. /* Statically defined subtrees to monitor */
  604. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc);
  605. if (err) { result = uid_op_error(53); break; }
  606. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv);
  607. if (err) { result = uid_op_error(54); break; }
  608. argc--; /* First argument was attribute name */
  609. argv++;
  610. } else if (0 != result)
  611. {
  612. break;
  613. }
  614. err = slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  615. if (err) { result = uid_op_error(61); break; }
  616. /* There may be more than one mod that matches e.g.
  617. changetype: modify
  618. delete: uid
  619. uid: balster1950
  620. -
  621. add: uid
  622. uid: scottg
  623. So, we need to first find all mods that contain the attribute
  624. which are add or replace ops and are bvalue encoded
  625. */
  626. /* find out how many mods meet this criteria */
  627. for(;*mods;mods++)
  628. {
  629. mod = *mods;
  630. if ((slapi_attr_type_cmp(mod->mod_type, attrName, 1) == 0) && /* mod contains target attr */
  631. (mod->mod_op & LDAP_MOD_BVALUES) && /* mod is bval encoded (not string val) */
  632. (mod->mod_bvalues && mod->mod_bvalues[0]) && /* mod actually contains some values */
  633. (SLAPI_IS_MOD_ADD(mod->mod_op) || /* mod is add */
  634. SLAPI_IS_MOD_REPLACE(mod->mod_op))) /* mod is replace */
  635. {
  636. addMod(&checkmods, &checkmodsCapacity, &modcount, mod);
  637. }
  638. }
  639. if (modcount == 0) {
  640. break; /* no mods to check, we are done */
  641. }
  642. /* Get the target DN */
  643. err = slapi_pblock_get(pb, SLAPI_MODIFY_TARGET, &dn);
  644. if (err) { result = uid_op_error(11); break; }
  645. if (requiredObjectClass &&
  646. !(spb = dnHasObjectClass(dn, requiredObjectClass))) { break; }
  647. /*
  648. * Passed all the requirements - this is an operation we
  649. * need to enforce uniqueness on. Now find all parent entries
  650. * with the marker object class, and do a search for each one.
  651. */
  652. /*
  653. * stop checking at first mod that fails the check
  654. */
  655. for (ii = 0; (result == 0) && (ii < modcount); ++ii)
  656. {
  657. mod = checkmods[ii];
  658. if (NULL != markerObjectClass)
  659. {
  660. /* Subtree defined by location of marker object class */
  661. result = findSubtreeAndSearch(dn, attrName, NULL,
  662. mod->mod_bvalues, dn,
  663. markerObjectClass);
  664. } else
  665. {
  666. /* Subtrees listed on invocation line */
  667. result = searchAllSubtrees(argc, argv, attrName, NULL,
  668. mod->mod_bvalues, dn);
  669. }
  670. }
  671. END
  672. slapi_ch_free((void **)&checkmods);
  673. freePblock(spb);
  674. if (result)
  675. {
  676. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  677. "MODIFY result %d\n", result);
  678. slapi_send_ldap_result(pb, result, 0, moreInfo, 0, 0);
  679. }
  680. return (result==LDAP_SUCCESS)?0:-1;
  681. }
  682. /* ------------------------------------------------------------ */
  683. /*
  684. * preop_modrdn - Pre-operation call for modify RDN
  685. *
  686. * Check that the new RDN does not include attributes that
  687. * cause a constraint violation
  688. */
  689. static int
  690. preop_modrdn(Slapi_PBlock *pb)
  691. {
  692. int result;
  693. Slapi_Entry *e;
  694. #ifdef DEBUG
  695. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  696. "MODRDN begin\n");
  697. #endif
  698. /* Init */
  699. result = LDAP_SUCCESS;
  700. e = 0;
  701. BEGIN
  702. int err;
  703. char *attrName = NULL;
  704. char *markerObjectClass=NULL;
  705. char *requiredObjectClass=NULL;
  706. char *dn;
  707. char *superior;
  708. char *rdn;
  709. int isupdatedn;
  710. Slapi_Attr *attr;
  711. int argc;
  712. char **argv = NULL;
  713. /*
  714. * If this is a replication update, just be a noop.
  715. */
  716. err = slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &isupdatedn);
  717. if (err) { result = uid_op_error(30); break; }
  718. if (isupdatedn)
  719. {
  720. break;
  721. }
  722. /*
  723. * Get the arguments
  724. */
  725. result = getArguments(pb, &attrName, &markerObjectClass,
  726. &requiredObjectClass);
  727. if (UNTAGGED_PARAMETER == result)
  728. {
  729. result = LDAP_SUCCESS;
  730. /* Statically defined subtrees to monitor */
  731. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc);
  732. if (err) { result = uid_op_error(53); break; }
  733. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv);
  734. if (err) { result = uid_op_error(54); break; }
  735. argc--; /* First argument was attribute name */
  736. argv++;
  737. } else if (0 != result)
  738. {
  739. break;
  740. }
  741. /* Get the DN of the entry being renamed */
  742. err = slapi_pblock_get(pb, SLAPI_MODRDN_TARGET, &dn);
  743. if (err) { result = uid_op_error(31); break; }
  744. /* Get superior value - unimplemented in 3.0/4.0/5.0 DS */
  745. err = slapi_pblock_get(pb, SLAPI_MODRDN_NEWSUPERIOR, &superior);
  746. if (err) { result = uid_op_error(32); break; }
  747. /*
  748. * No superior means the entry is just renamed at
  749. * its current level in the tree. Use the target DN for
  750. * determining which managed tree this belongs to
  751. */
  752. if (!superior) superior = dn;
  753. /* Get the new RDN - this has the attribute values */
  754. err = slapi_pblock_get(pb, SLAPI_MODRDN_NEWRDN, &rdn);
  755. if (err) { result = uid_op_error(33); break; }
  756. #ifdef DEBUG
  757. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  758. "MODRDN newrdn=%s\n", rdn);
  759. #endif
  760. /*
  761. * Parse the RDN into attributes by creating a "dummy" entry
  762. * and setting the attributes from the RDN.
  763. *
  764. * The new entry must be freed.
  765. */
  766. e = slapi_entry_alloc();
  767. if (!e) { result = uid_op_error(34); break; }
  768. /* NOTE: strdup on the rdn, since it will be freed when
  769. * the entry is freed */
  770. slapi_entry_set_dn(e, slapi_ch_strdup(rdn));
  771. err = slapi_entry_add_rdn_values(e);
  772. if (err)
  773. {
  774. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  775. "MODRDN bad rdn value=%s\n", rdn);
  776. break; /* Bad DN */
  777. }
  778. /*
  779. * Find any unique attribute data in the new RDN
  780. */
  781. err = slapi_entry_attr_find(e, attrName, &attr);
  782. if (err) break; /* no UID attribute */
  783. /*
  784. * Passed all the requirements - this is an operation we
  785. * need to enforce uniqueness on. Now find all parent entries
  786. * with the marker object class, and do a search for each one.
  787. */
  788. if (NULL != markerObjectClass)
  789. {
  790. /* Subtree defined by location of marker object class */
  791. result = findSubtreeAndSearch(dn, attrName, attr, NULL, dn,
  792. markerObjectClass);
  793. } else
  794. {
  795. /* Subtrees listed on invocation line */
  796. result = searchAllSubtrees(argc, argv, attrName, attr, NULL, dn);
  797. }
  798. END
  799. /* Clean-up */
  800. if (e) slapi_entry_free(e);
  801. if (result)
  802. {
  803. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  804. "MODRDN result %d\n", result);
  805. slapi_send_ldap_result(pb, result, 0, moreInfo, 0, 0);
  806. }
  807. return (result==LDAP_SUCCESS)?0:-1;
  808. }
  809. /* ------------------------------------------------------------ */
  810. /*
  811. * Initialize the plugin
  812. *
  813. * uidunique_init (the old name) is deprecated
  814. */
  815. int
  816. NSUniqueAttr_Init(Slapi_PBlock *pb)
  817. {
  818. int err = 0;
  819. BEGIN
  820. int argc;
  821. char **argv;
  822. /* Declare plugin version */
  823. err = slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  824. SLAPI_PLUGIN_VERSION_01);
  825. if (err) break;
  826. /*
  827. * Get plugin identity and store it for later use
  828. * Used for internal operations
  829. */
  830. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &plugin_identity);
  831. /* PR_ASSERT (plugin_identity); */
  832. /*
  833. * Get and normalize arguments
  834. */
  835. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc);
  836. if (err) break;
  837. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv);
  838. if (err) break;
  839. /* First argument is the unique attribute name */
  840. if (argc < 1) { err = -1; break; }
  841. argv++; argc--;
  842. for(;argc > 0;argc--, argv++)
  843. slapi_dn_normalize_case(*argv);
  844. /* Provide descriptive information */
  845. err = slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  846. (void*)&pluginDesc);
  847. if (err) break;
  848. /* Register functions */
  849. err = slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_ADD_FN,
  850. (void*)preop_add);
  851. if (err) break;
  852. err = slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_MODIFY_FN,
  853. (void*)preop_modify);
  854. if (err) break;
  855. err = slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_MODRDN_FN,
  856. (void*)preop_modrdn);
  857. if (err) break;
  858. END
  859. if (err) {
  860. slapi_log_error(SLAPI_LOG_PLUGIN, "NSUniqueAttr_Init",
  861. "Error: %d\n", err);
  862. err = -1;
  863. }
  864. else
  865. slapi_log_error(SLAPI_LOG_PLUGIN, "NSUniqueAttr_Init",
  866. "plugin loaded\n");
  867. return err;
  868. }
  869. int
  870. uidunique_init(Slapi_PBlock *pb)
  871. {
  872. return NSUniqueAttr_Init(pb);
  873. }
  874. /* ------------------------------------------------------------ */
  875. /*
  876. * ldap_quote_filter_value
  877. *
  878. * Quote the filter value according to RFC 2254 (Dec 1997)
  879. *
  880. * value - a UTF8 string containing the value. It may contain
  881. * any of the chars needing quotes ( '(' ')' '*' '/' and NUL ).
  882. * len - the length of the UTF8 value
  883. * out - a buffer to recieve the converted value. May be NULL, in
  884. * which case, only the length of the output is computed (and placed in
  885. * outLen).
  886. * maxLen - the size of the output buffer. It is an error if this length
  887. * is exceeded. Ignored if out is NULL.
  888. * outLen - recieves the size of the output. If an error occurs, this
  889. * result is not available.
  890. *
  891. * Returns
  892. * 0 - success
  893. * -1 - failure (usually a buffer overflow)
  894. */
  895. int /* Error value */
  896. ldap_quote_filter_value(
  897. char *value, int len,
  898. char *out, int maxLen,
  899. int *outLen)
  900. {
  901. int err;
  902. char *eValue;
  903. int resLen;
  904. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  905. static char hexchars[16] = "0123456789abcdef";
  906. #endif
  907. err = 0;
  908. eValue = &value[len];
  909. resLen = 0;
  910. /*
  911. * Convert each character in the input string
  912. */
  913. while(value < eValue)
  914. {
  915. switch(*value)
  916. {
  917. case '(':
  918. case ')':
  919. case '*':
  920. case '\\':
  921. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  922. case 0:
  923. #endif
  924. /* Handle characters needing special escape sequences */
  925. /* Compute size of output */
  926. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  927. resLen += 3;
  928. #else
  929. resLen += 2;
  930. #endif
  931. /* Generate output if requested */
  932. if (out)
  933. {
  934. /* Check for overflow */
  935. if (resLen > maxLen) { err = -1; break; }
  936. *out++ = '\\';
  937. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  938. *out++ = hexchars[(*value >> 4) & 0xF];
  939. *out++ = hexchars[*value & 0xF];
  940. #else
  941. *out++ = *value;
  942. #endif
  943. }
  944. break;
  945. default:
  946. /* Compute size of output */
  947. resLen += 1;
  948. /* Generate output if requested */
  949. if (out)
  950. {
  951. if (resLen > maxLen) { err = -1; break; }
  952. *out++ = *value;
  953. }
  954. break;
  955. }
  956. if (err) break;
  957. value++;
  958. }
  959. if (!err) *outLen = resLen;
  960. return err;
  961. }