uid.c 30 KB

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