uid.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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. result = search(*argv, attrName, attr, values, dn);
  330. if (result) break;
  331. }
  332. return result;
  333. }
  334. /* ------------------------------------------------------------ */
  335. /*
  336. * getArguments - parse invocation parameters
  337. * Return:
  338. * 0 - success
  339. * >0 - error parsing parameters
  340. */
  341. static int
  342. getArguments(Slapi_PBlock *pb, char **attrName, char **markerObjectClass,
  343. char **requiredObjectClass)
  344. {
  345. int argc;
  346. char **argv;
  347. /*
  348. * Get the arguments
  349. */
  350. if (slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc))
  351. {
  352. return uid_op_error(10);
  353. }
  354. if (slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv))
  355. {
  356. return uid_op_error(11);
  357. }
  358. /*
  359. * Required arguments: attribute and markerObjectClass
  360. * Optional argument: requiredObjectClass
  361. */
  362. for(;argc > 0;argc--,argv++)
  363. {
  364. char *param = *argv;
  365. char *delimiter = strchr(param, '=');
  366. if (NULL == delimiter)
  367. {
  368. /* Old style untagged parameter */
  369. *attrName = *argv;
  370. return UNTAGGED_PARAMETER;
  371. }
  372. if (strncasecmp(param, "attribute", delimiter-param) == 0)
  373. {
  374. /* It's OK to set a pointer here, because ultimately it points
  375. * inside the argv array of the pblock, which will be staying
  376. * arround.
  377. */
  378. *attrName = delimiter+1;
  379. } else if (strncasecmp(param, "markerobjectclass", delimiter-param) == 0)
  380. {
  381. *markerObjectClass = delimiter+1;
  382. } else if (strncasecmp(param, "requiredobjectclass", delimiter-param) == 0)
  383. {
  384. *requiredObjectClass = delimiter+1;
  385. }
  386. }
  387. if (!*attrName || !*markerObjectClass)
  388. {
  389. return uid_op_error(13);
  390. }
  391. return 0;
  392. }
  393. /* ------------------------------------------------------------ */
  394. /*
  395. * findSubtreeAndSearch - walk up the tree to find an entry with
  396. * the marker object class; if found, call search from there and
  397. * return the result it returns
  398. *
  399. * If 'attr' is NULL, the values are taken from 'values'.
  400. * If 'attr' is non-NULL, the values are taken from 'attr'.
  401. *
  402. * Return:
  403. * LDAP_SUCCESS - no matches, or the attribute matches the
  404. * target dn.
  405. * LDAP_CONSTRAINT_VIOLATION - an entry was found that already
  406. * contains the attribute value.
  407. * LDAP_OPERATIONS_ERROR - a server failure.
  408. */
  409. static int
  410. findSubtreeAndSearch(char *parentDN, const char *attrName, Slapi_Attr *attr,
  411. struct berval **values, const char *target, const char *markerObjectClass)
  412. {
  413. int result = LDAP_SUCCESS;
  414. Slapi_PBlock *spb = NULL;
  415. while (NULL != (parentDN = slapi_dn_parent(parentDN)))
  416. {
  417. if ((spb = dnHasObjectClass(parentDN, markerObjectClass)))
  418. {
  419. freePblock(spb);
  420. /*
  421. * Do the search. There is no entry that is allowed
  422. * to have the attribute already.
  423. */
  424. result = search(parentDN, attrName, attr, values, target);
  425. break;
  426. }
  427. }
  428. return result;
  429. }
  430. /* ------------------------------------------------------------ */
  431. /*
  432. * preop_add - pre-operation plug-in for add
  433. */
  434. static int
  435. preop_add(Slapi_PBlock *pb)
  436. {
  437. int result;
  438. #ifdef DEBUG
  439. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name, "ADD begin\n");
  440. #endif
  441. result = LDAP_SUCCESS;
  442. /*
  443. * Do constraint check on the added entry. Set result.
  444. */
  445. BEGIN
  446. int err;
  447. char *attrName = NULL;
  448. char *markerObjectClass = NULL;
  449. char *requiredObjectClass = NULL;
  450. char *dn;
  451. int isupdatedn;
  452. Slapi_Entry *e;
  453. Slapi_Attr *attr;
  454. int argc;
  455. char **argv = NULL;
  456. /*
  457. * If this is a replication update, just be a noop.
  458. */
  459. err = slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &isupdatedn);
  460. if (err) { result = uid_op_error(50); break; }
  461. if (isupdatedn)
  462. {
  463. break;
  464. }
  465. /*
  466. * Get the arguments
  467. */
  468. result = getArguments(pb, &attrName, &markerObjectClass,
  469. &requiredObjectClass);
  470. if (UNTAGGED_PARAMETER == result)
  471. {
  472. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  473. "ADD parameter untagged: %s\n", attrName);
  474. result = LDAP_SUCCESS;
  475. /* Statically defined subtrees to monitor */
  476. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc);
  477. if (err) { result = uid_op_error(53); break; }
  478. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv);
  479. if (err) { result = uid_op_error(54); break; }
  480. argc--; argv++; /* First argument was attribute name */
  481. } else if (0 != result)
  482. {
  483. break;
  484. }
  485. /*
  486. * Get the target DN for this add operation
  487. */
  488. err = slapi_pblock_get(pb, SLAPI_ADD_TARGET, &dn);
  489. if (err) { result = uid_op_error(51); break; }
  490. #ifdef DEBUG
  491. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name, "ADD target=%s\n", dn);
  492. #endif
  493. /*
  494. * Get the entry data for this add. Check whether it
  495. * contains a value for the unique attribute
  496. */
  497. err = slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e);
  498. if (err) { result = uid_op_error(52); break; }
  499. err = slapi_entry_attr_find(e, attrName, &attr);
  500. if (err) break; /* no unique attribute */
  501. /*
  502. * Check if it contains the required object class
  503. */
  504. if (NULL != requiredObjectClass)
  505. {
  506. if (!entryHasObjectClass(pb, e, requiredObjectClass))
  507. {
  508. /* No, so we don't have to do anything */
  509. break;
  510. }
  511. }
  512. /*
  513. * Passed all the requirements - this is an operation we
  514. * need to enforce uniqueness on. Now find all parent entries
  515. * with the marker object class, and do a search for each one.
  516. */
  517. if (NULL != markerObjectClass)
  518. {
  519. /* Subtree defined by location of marker object class */
  520. result = findSubtreeAndSearch(dn, attrName, attr, NULL,
  521. dn, markerObjectClass);
  522. } else
  523. {
  524. /* Subtrees listed on invocation line */
  525. result = searchAllSubtrees(argc, argv, attrName, attr, NULL, dn);
  526. }
  527. END
  528. if (result)
  529. {
  530. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  531. "ADD result %d\n", result);
  532. /* Send failure to the client */
  533. slapi_send_ldap_result(pb, result, 0, moreInfo, 0, 0);
  534. }
  535. return (result==LDAP_SUCCESS)?0:-1;
  536. }
  537. static void
  538. addMod(LDAPMod ***modary, int *capacity, int *nmods, LDAPMod *toadd)
  539. {
  540. if (*nmods == *capacity) {
  541. *capacity += 4;
  542. if (*modary) {
  543. *modary = (LDAPMod **)slapi_ch_realloc((char *)*modary, *capacity * sizeof(LDAPMod *));
  544. } else {
  545. *modary = (LDAPMod **)slapi_ch_malloc(*capacity * sizeof(LDAPMod *));
  546. }
  547. }
  548. *modary[*nmods] = toadd;
  549. (*nmods)++;
  550. }
  551. /* ------------------------------------------------------------ */
  552. /*
  553. * preop_modify - pre-operation plug-in for modify
  554. */
  555. static int
  556. preop_modify(Slapi_PBlock *pb)
  557. {
  558. int result = LDAP_SUCCESS;
  559. Slapi_PBlock *spb = NULL;
  560. LDAPMod **checkmods = NULL;
  561. int checkmodsCapacity = 0;
  562. #ifdef DEBUG
  563. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  564. "MODIFY begin\n");
  565. #endif
  566. BEGIN
  567. int err;
  568. char *attrName;
  569. char *markerObjectClass=NULL;
  570. char *requiredObjectClass=NULL;
  571. LDAPMod **mods;
  572. int modcount = 0;
  573. int ii;
  574. LDAPMod *mod;
  575. char *dn;
  576. int isupdatedn;
  577. int argc;
  578. char **argv = NULL;
  579. /*
  580. * If this is a replication update, just be a noop.
  581. */
  582. err = slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &isupdatedn);
  583. if (err) { result = uid_op_error(60); break; }
  584. if (isupdatedn)
  585. {
  586. break;
  587. }
  588. /*
  589. * Get the arguments
  590. */
  591. result = getArguments(pb, &attrName, &markerObjectClass,
  592. &requiredObjectClass);
  593. if (UNTAGGED_PARAMETER == result)
  594. {
  595. result = LDAP_SUCCESS;
  596. /* Statically defined subtrees to monitor */
  597. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc);
  598. if (err) { result = uid_op_error(53); break; }
  599. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv);
  600. if (err) { result = uid_op_error(54); break; }
  601. argc--; /* First argument was attribute name */
  602. argv++;
  603. } else if (0 != result)
  604. {
  605. break;
  606. }
  607. err = slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  608. if (err) { result = uid_op_error(61); break; }
  609. /* There may be more than one mod that matches e.g.
  610. changetype: modify
  611. delete: uid
  612. uid: balster1950
  613. -
  614. add: uid
  615. uid: scottg
  616. So, we need to first find all mods that contain the attribute
  617. which are add or replace ops and are bvalue encoded
  618. */
  619. /* find out how many mods meet this criteria */
  620. for(;*mods;mods++)
  621. {
  622. mod = *mods;
  623. if ((slapi_attr_type_cmp(mod->mod_type, attrName, 1) == 0) && /* mod contains target attr */
  624. (mod->mod_op & LDAP_MOD_BVALUES) && /* mod is bval encoded (not string val) */
  625. (mod->mod_bvalues && mod->mod_bvalues[0]) && /* mod actually contains some values */
  626. (((mod->mod_op & ~LDAP_MOD_BVALUES) == LDAP_MOD_ADD) || /* mod is add */
  627. (mod->mod_op & LDAP_MOD_REPLACE))) /* mod is replace */
  628. {
  629. addMod(&checkmods, &checkmodsCapacity, &modcount, mod);
  630. }
  631. }
  632. if (modcount == 0) {
  633. break; /* no mods to check, we are done */
  634. }
  635. /* Get the target DN */
  636. err = slapi_pblock_get(pb, SLAPI_MODIFY_TARGET, &dn);
  637. if (err) { result = uid_op_error(11); break; }
  638. if (requiredObjectClass &&
  639. !(spb = dnHasObjectClass(dn, requiredObjectClass))) { break; }
  640. /*
  641. * Passed all the requirements - this is an operation we
  642. * need to enforce uniqueness on. Now find all parent entries
  643. * with the marker object class, and do a search for each one.
  644. */
  645. /*
  646. * stop checking at first mod that fails the check
  647. */
  648. for (ii = 0; (result == 0) && (ii < modcount); ++ii)
  649. {
  650. mod = checkmods[ii];
  651. if (NULL != markerObjectClass)
  652. {
  653. /* Subtree defined by location of marker object class */
  654. result = findSubtreeAndSearch(dn, attrName, NULL,
  655. mod->mod_bvalues, dn,
  656. markerObjectClass);
  657. } else
  658. {
  659. /* Subtrees listed on invocation line */
  660. result = searchAllSubtrees(argc, argv, attrName, NULL,
  661. mod->mod_bvalues, dn);
  662. }
  663. }
  664. END
  665. slapi_ch_free((void **)&checkmods);
  666. freePblock(spb);
  667. if (result)
  668. {
  669. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  670. "MODIFY result %d\n", result);
  671. slapi_send_ldap_result(pb, result, 0, moreInfo, 0, 0);
  672. }
  673. return (result==LDAP_SUCCESS)?0:-1;
  674. }
  675. /* ------------------------------------------------------------ */
  676. /*
  677. * preop_modrdn - Pre-operation call for modify RDN
  678. *
  679. * Check that the new RDN does not include attributes that
  680. * cause a constraint violation
  681. */
  682. static int
  683. preop_modrdn(Slapi_PBlock *pb)
  684. {
  685. int result;
  686. Slapi_Entry *e;
  687. #ifdef DEBUG
  688. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  689. "MODRDN begin\n");
  690. #endif
  691. /* Init */
  692. result = LDAP_SUCCESS;
  693. e = 0;
  694. BEGIN
  695. int err;
  696. char *attrName;
  697. char *markerObjectClass=NULL;
  698. char *requiredObjectClass=NULL;
  699. char *dn;
  700. char *superior;
  701. char *rdn;
  702. int isupdatedn;
  703. Slapi_Attr *attr;
  704. int argc;
  705. char **argv = NULL;
  706. /*
  707. * If this is a replication update, just be a noop.
  708. */
  709. err = slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &isupdatedn);
  710. if (err) { result = uid_op_error(30); break; }
  711. if (isupdatedn)
  712. {
  713. break;
  714. }
  715. /*
  716. * Get the arguments
  717. */
  718. result = getArguments(pb, &attrName, &markerObjectClass,
  719. &requiredObjectClass);
  720. if (UNTAGGED_PARAMETER == result)
  721. {
  722. result = LDAP_SUCCESS;
  723. /* Statically defined subtrees to monitor */
  724. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc);
  725. if (err) { result = uid_op_error(53); break; }
  726. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv);
  727. if (err) { result = uid_op_error(54); break; }
  728. argc--; /* First argument was attribute name */
  729. argv++;
  730. } else if (0 != result)
  731. {
  732. break;
  733. }
  734. /* Get the DN of the entry being renamed */
  735. err = slapi_pblock_get(pb, SLAPI_MODRDN_TARGET, &dn);
  736. if (err) { result = uid_op_error(31); break; }
  737. /* Get superior value - unimplemented in 3.0/4.0/5.0 DS */
  738. err = slapi_pblock_get(pb, SLAPI_MODRDN_NEWSUPERIOR, &superior);
  739. if (err) { result = uid_op_error(32); break; }
  740. /*
  741. * No superior means the entry is just renamed at
  742. * its current level in the tree. Use the target DN for
  743. * determining which managed tree this belongs to
  744. */
  745. if (!superior) superior = dn;
  746. /* Get the new RDN - this has the attribute values */
  747. err = slapi_pblock_get(pb, SLAPI_MODRDN_NEWRDN, &rdn);
  748. if (err) { result = uid_op_error(33); break; }
  749. #ifdef DEBUG
  750. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  751. "MODRDN newrdn=%s\n", rdn);
  752. #endif
  753. /*
  754. * Parse the RDN into attributes by creating a "dummy" entry
  755. * and setting the attributes from the RDN.
  756. *
  757. * The new entry must be freed.
  758. */
  759. e = slapi_entry_alloc();
  760. if (!e) { result = uid_op_error(34); break; }
  761. /* NOTE: strdup on the rdn, since it will be freed when
  762. * the entry is freed */
  763. slapi_entry_set_dn(e, slapi_ch_strdup(rdn));
  764. err = slapi_entry_add_rdn_values(e);
  765. if (err)
  766. {
  767. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  768. "MODRDN bad rdn value=%s\n", rdn);
  769. break; /* Bad DN */
  770. }
  771. /*
  772. * Find any unique attribute data in the new RDN
  773. */
  774. err = slapi_entry_attr_find(e, attrName, &attr);
  775. if (err) break; /* no UID attribute */
  776. /*
  777. * Passed all the requirements - this is an operation we
  778. * need to enforce uniqueness on. Now find all parent entries
  779. * with the marker object class, and do a search for each one.
  780. */
  781. if (NULL != markerObjectClass)
  782. {
  783. /* Subtree defined by location of marker object class */
  784. result = findSubtreeAndSearch(dn, attrName, attr, NULL, dn,
  785. markerObjectClass);
  786. } else
  787. {
  788. /* Subtrees listed on invocation line */
  789. result = searchAllSubtrees(argc, argv, attrName, attr, NULL, dn);
  790. }
  791. END
  792. /* Clean-up */
  793. if (e) slapi_entry_free(e);
  794. if (result)
  795. {
  796. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  797. "MODRDN result %d\n", result);
  798. slapi_send_ldap_result(pb, result, 0, moreInfo, 0, 0);
  799. }
  800. return (result==LDAP_SUCCESS)?0:-1;
  801. }
  802. /* ------------------------------------------------------------ */
  803. /*
  804. * Initialize the plugin
  805. *
  806. * uidunique_init (the old name) is deprecated
  807. */
  808. int
  809. NSUniqueAttr_Init(Slapi_PBlock *pb)
  810. {
  811. int err = 0;
  812. BEGIN
  813. int err;
  814. int argc;
  815. char **argv;
  816. /* Declare plugin version */
  817. err = slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  818. SLAPI_PLUGIN_VERSION_01);
  819. if (err) break;
  820. /*
  821. * Get plugin identity and store it for later use
  822. * Used for internal operations
  823. */
  824. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &plugin_identity);
  825. /* PR_ASSERT (plugin_identity); */
  826. /*
  827. * Get and normalize arguments
  828. */
  829. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc);
  830. if (err) break;
  831. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv);
  832. if (err) break;
  833. /* First argument is the unique attribute name */
  834. if (argc < 1) { err = -1; break; }
  835. argv++; argc--;
  836. for(;argc > 0;argc--, argv++)
  837. slapi_dn_normalize_case(*argv);
  838. /* Provide descriptive information */
  839. err = slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  840. (void*)&pluginDesc);
  841. if (err) break;
  842. /* Register functions */
  843. err = slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_ADD_FN,
  844. (void*)preop_add);
  845. if (err) break;
  846. err = slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_MODIFY_FN,
  847. (void*)preop_modify);
  848. if (err) break;
  849. err = slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_MODRDN_FN,
  850. (void*)preop_modrdn);
  851. if (err) break;
  852. END
  853. if (err) {
  854. slapi_log_error(SLAPI_LOG_PLUGIN, "NSUniqueAttr_Init",
  855. "Error: %d\n", err);
  856. err = -1;
  857. }
  858. else
  859. slapi_log_error(SLAPI_LOG_PLUGIN, "NSUniqueAttr_Init",
  860. "plugin loaded\n");
  861. return err;
  862. }
  863. int
  864. uidunique_init(Slapi_PBlock *pb)
  865. {
  866. return NSUniqueAttr_Init(pb);
  867. }
  868. /* ------------------------------------------------------------ */
  869. /*
  870. * ldap_quote_filter_value
  871. *
  872. * Quote the filter value according to RFC 2254 (Dec 1997)
  873. *
  874. * value - a UTF8 string containing the value. It may contain
  875. * any of the chars needing quotes ( '(' ')' '*' '/' and NUL ).
  876. * len - the length of the UTF8 value
  877. * out - a buffer to recieve the converted value. May be NULL, in
  878. * which case, only the length of the output is computed (and placed in
  879. * outLen).
  880. * maxLen - the size of the output buffer. It is an error if this length
  881. * is exceeded. Ignored if out is NULL.
  882. * outLen - recieves the size of the output. If an error occurs, this
  883. * result is not available.
  884. *
  885. * Returns
  886. * 0 - success
  887. * -1 - failure (usually a buffer overflow)
  888. */
  889. int /* Error value */
  890. ldap_quote_filter_value(
  891. char *value, int len,
  892. char *out, int maxLen,
  893. int *outLen)
  894. {
  895. int err;
  896. char *eValue;
  897. int resLen;
  898. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  899. static char hexchars[16] = "0123456789abcdef";
  900. #endif
  901. err = 0;
  902. eValue = &value[len];
  903. resLen = 0;
  904. /*
  905. * Convert each character in the input string
  906. */
  907. while(value < eValue)
  908. {
  909. switch(*value)
  910. {
  911. case '(':
  912. case ')':
  913. case '*':
  914. case '\\':
  915. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  916. case 0:
  917. #endif
  918. /* Handle characters needing special escape sequences */
  919. /* Compute size of output */
  920. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  921. resLen += 3;
  922. #else
  923. resLen += 2;
  924. #endif
  925. /* Generate output if requested */
  926. if (out)
  927. {
  928. /* Check for overflow */
  929. if (resLen > maxLen) { err = -1; break; }
  930. *out++ = '\\';
  931. #ifdef SLAPI_SUPPORTS_V3_ESCAPING
  932. *out++ = hexchars[(*value >> 4) & 0xF];
  933. *out++ = hexchars[*value & 0xF];
  934. #else
  935. *out++ = *value;
  936. #endif
  937. }
  938. break;
  939. default:
  940. /* Compute size of output */
  941. resLen += 1;
  942. /* Generate output if requested */
  943. if (out)
  944. {
  945. if (resLen > maxLen) { err = -1; break; }
  946. *out++ = *value;
  947. }
  948. break;
  949. }
  950. if (err) break;
  951. value++;
  952. }
  953. if (!err) *outLen = resLen;
  954. return err;
  955. }