uid.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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;
  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;
  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 err;
  821. int argc;
  822. char **argv;
  823. /* Declare plugin version */
  824. err = slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  825. SLAPI_PLUGIN_VERSION_01);
  826. if (err) break;
  827. /*
  828. * Get plugin identity and store it for later use
  829. * Used for internal operations
  830. */
  831. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &plugin_identity);
  832. /* PR_ASSERT (plugin_identity); */
  833. /*
  834. * Get and normalize arguments
  835. */
  836. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc);
  837. if (err) break;
  838. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv);
  839. if (err) break;
  840. /* First argument is the unique attribute name */
  841. if (argc < 1) { err = -1; break; }
  842. argv++; argc--;
  843. for(;argc > 0;argc--, argv++)
  844. slapi_dn_normalize_case(*argv);
  845. /* Provide descriptive information */
  846. err = slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  847. (void*)&pluginDesc);
  848. if (err) break;
  849. /* Register functions */
  850. err = slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_ADD_FN,
  851. (void*)preop_add);
  852. if (err) break;
  853. err = slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_MODIFY_FN,
  854. (void*)preop_modify);
  855. if (err) break;
  856. err = slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_MODRDN_FN,
  857. (void*)preop_modrdn);
  858. if (err) break;
  859. END
  860. if (err) {
  861. slapi_log_error(SLAPI_LOG_PLUGIN, "NSUniqueAttr_Init",
  862. "Error: %d\n", err);
  863. err = -1;
  864. }
  865. else
  866. slapi_log_error(SLAPI_LOG_PLUGIN, "NSUniqueAttr_Init",
  867. "plugin loaded\n");
  868. return err;
  869. }
  870. int
  871. uidunique_init(Slapi_PBlock *pb)
  872. {
  873. return NSUniqueAttr_Init(pb);
  874. }
  875. /* ------------------------------------------------------------ */
  876. /*
  877. * ldap_quote_filter_value
  878. *
  879. * Quote the filter value according to RFC 2254 (Dec 1997)
  880. *
  881. * value - a UTF8 string containing the value. It may contain
  882. * any of the chars needing quotes ( '(' ')' '*' '/' and NUL ).
  883. * len - the length of the UTF8 value
  884. * out - a buffer to recieve the converted value. May be NULL, in
  885. * which case, only the length of the output is computed (and placed in
  886. * outLen).
  887. * maxLen - the size of the output buffer. It is an error if this length
  888. * is exceeded. Ignored if out is NULL.
  889. * outLen - recieves the size of the output. If an error occurs, this
  890. * result is not available.
  891. *
  892. * Returns
  893. * 0 - success
  894. * -1 - failure (usually a buffer overflow)
  895. */
  896. int /* Error value */
  897. ldap_quote_filter_value(
  898. char *value, int len,
  899. char *out, int maxLen,
  900. int *outLen)
  901. {
  902. int err;
  903. char *eValue;
  904. int resLen;
  905. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  906. static char hexchars[16] = "0123456789abcdef";
  907. #endif
  908. err = 0;
  909. eValue = &value[len];
  910. resLen = 0;
  911. /*
  912. * Convert each character in the input string
  913. */
  914. while(value < eValue)
  915. {
  916. switch(*value)
  917. {
  918. case '(':
  919. case ')':
  920. case '*':
  921. case '\\':
  922. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  923. case 0:
  924. #endif
  925. /* Handle characters needing special escape sequences */
  926. /* Compute size of output */
  927. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  928. resLen += 3;
  929. #else
  930. resLen += 2;
  931. #endif
  932. /* Generate output if requested */
  933. if (out)
  934. {
  935. /* Check for overflow */
  936. if (resLen > maxLen) { err = -1; break; }
  937. *out++ = '\\';
  938. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  939. *out++ = hexchars[(*value >> 4) & 0xF];
  940. *out++ = hexchars[*value & 0xF];
  941. #else
  942. *out++ = *value;
  943. #endif
  944. }
  945. break;
  946. default:
  947. /* Compute size of output */
  948. resLen += 1;
  949. /* Generate output if requested */
  950. if (out)
  951. {
  952. if (resLen > maxLen) { err = -1; break; }
  953. *out++ = *value;
  954. }
  955. break;
  956. }
  957. if (err) break;
  958. value++;
  959. }
  960. if (!err) *outLen = resLen;
  961. return err;
  962. }