uid.c 30 KB

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