findentry.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. /* findentry.c - find a database entry, obeying referrals (& aliases?) */
  42. #include "back-ldbm.h"
  43. int
  44. check_entry_for_referral(Slapi_PBlock *pb, Slapi_Entry *entry, char *matched, const char *callingfn) /* JCM - Move somewhere more appropriate */
  45. {
  46. int rc=0, i=0, numValues=0;
  47. Slapi_Attr *attr;
  48. /* if the entry is a referral send the referral */
  49. if ( slapi_entry_attr_find( entry, "ref", &attr ) == 0 )
  50. {
  51. Slapi_Value *val=NULL;
  52. struct berval **refscopy=NULL;
  53. struct berval **url=NULL;
  54. slapi_attr_get_numvalues(attr, &numValues );
  55. if(numValues > 0) {
  56. url=(struct berval **) slapi_ch_malloc((numValues + 1) * sizeof(struct berval*));
  57. }
  58. for (i = slapi_attr_first_value(attr, &val); i != -1;
  59. i = slapi_attr_next_value(attr, i, &val)) {
  60. url[i]=(struct berval*)slapi_value_get_berval(val);
  61. }
  62. url[numValues]=NULL;
  63. refscopy = ref_adjust( pb, url, slapi_entry_get_sdn(entry), 0 ); /* JCM - What's this PBlock* for? */
  64. slapi_send_ldap_result( pb, LDAP_REFERRAL, matched, NULL, 0, refscopy );
  65. LDAPDebug( LDAP_DEBUG_TRACE,
  66. "<= %s sent referral to (%s) for (%s)\n",
  67. callingfn,
  68. refscopy ? refscopy[0]->bv_val : "",
  69. slapi_entry_get_dn(entry));
  70. if ( refscopy != NULL )
  71. {
  72. ber_bvecfree( refscopy );
  73. }
  74. if( url != NULL) {
  75. slapi_ch_free( (void **)&url );
  76. }
  77. rc= 1;
  78. }
  79. return rc;
  80. }
  81. static struct backentry *
  82. find_entry_internal_dn(
  83. Slapi_PBlock *pb,
  84. backend *be,
  85. const Slapi_DN *sdn,
  86. int lock,
  87. back_txn *txn,
  88. int really_internal
  89. )
  90. {
  91. struct backentry *e;
  92. int managedsait = 0;
  93. int err;
  94. ldbm_instance *inst = (ldbm_instance *) be->be_instance_info;
  95. size_t tries = 0;
  96. /* get the managedsait ldap message control */
  97. slapi_pblock_get( pb, SLAPI_MANAGEDSAIT, &managedsait );
  98. while ( (tries < LDBM_CACHE_RETRY_COUNT) &&
  99. (e = dn2entry( be, sdn, txn, &err )) != NULL )
  100. {
  101. /*
  102. * we found the entry. if the managedsait control is set,
  103. * we return the entry. if managedsait is not set, we check
  104. * for the presence of a ref attribute, returning to the
  105. * client a referral to the ref'ed entry if a ref is present,
  106. * returning the entry to the caller if not.
  107. */
  108. if ( !managedsait && !really_internal) {
  109. /* see if the entry is a referral */
  110. if(check_entry_for_referral(pb, e->ep_entry, NULL, "find_entry_internal_dn"))
  111. {
  112. cache_return( &inst->inst_cache, &e );
  113. return( NULL );
  114. }
  115. }
  116. /*
  117. * we'd like to return the entry. lock it if requested,
  118. * retrying if necessary.
  119. */
  120. /* wait for entry modify lock */
  121. if ( !lock || cache_lock_entry( &inst->inst_cache, e ) == 0 ) {
  122. LDAPDebug( LDAP_DEBUG_TRACE,
  123. "<= find_entry_internal_dn found (%s)\n", slapi_sdn_get_dn(sdn), 0, 0 );
  124. return( e );
  125. }
  126. /*
  127. * this entry has been deleted - see if it was actually
  128. * replaced with a new copy, and try the whole thing again.
  129. */
  130. LDAPDebug( LDAP_DEBUG_ARGS,
  131. " find_entry_internal_dn retrying (%s)\n", slapi_sdn_get_dn(sdn), 0, 0 );
  132. cache_return( &inst->inst_cache, &e );
  133. tries++;
  134. }
  135. if (tries >= LDBM_CACHE_RETRY_COUNT) {
  136. LDAPDebug( LDAP_DEBUG_ANY,"find_entry_internal_dn retry count exceeded (%s)\n", slapi_sdn_get_dn(sdn), 0, 0 );
  137. }
  138. /*
  139. * there is no such entry in this server. see how far we
  140. * can match, and check if that entry contains a referral.
  141. * if it does and managedsait is not set, we return the
  142. * referral to the client. if it doesn't, or managedsait
  143. * is set, we return no such object.
  144. */
  145. if (!really_internal) {
  146. struct backentry *me;
  147. Slapi_DN ancestordn= {0};
  148. me= dn2ancestor(pb->pb_backend,sdn,&ancestordn,txn,&err);
  149. if ( !managedsait && me != NULL ) {
  150. /* if the entry is a referral send the referral */
  151. if(check_entry_for_referral(pb, me->ep_entry, (char*)slapi_sdn_get_dn(&ancestordn), "find_entry_internal_dn"))
  152. {
  153. cache_return( &inst->inst_cache, &me );
  154. slapi_sdn_done(&ancestordn);
  155. return( NULL );
  156. }
  157. /* else fall through to no such object */
  158. }
  159. /* entry not found */
  160. slapi_send_ldap_result( pb, ( 0 == err || DB_NOTFOUND == err ) ?
  161. LDAP_NO_SUCH_OBJECT : LDAP_OPERATIONS_ERROR, (char*)slapi_sdn_get_dn(&ancestordn), NULL,
  162. 0, NULL );
  163. slapi_sdn_done(&ancestordn);
  164. cache_return( &inst->inst_cache, &me );
  165. }
  166. LDAPDebug( LDAP_DEBUG_TRACE, "<= find_entry_internal_dn not found (%s)\n",
  167. slapi_sdn_get_dn(sdn), 0, 0 );
  168. return( NULL );
  169. }
  170. /* Note that this function does not issue any referals.
  171. It should only be called in case of 5.0 replicated operation
  172. which should not be referred.
  173. */
  174. static struct backentry *
  175. find_entry_internal_uniqueid(
  176. Slapi_PBlock *pb,
  177. backend *be,
  178. const char *uniqueid,
  179. int lock,
  180. back_txn *txn
  181. )
  182. {
  183. ldbm_instance *inst = (ldbm_instance *) be->be_instance_info;
  184. struct backentry *e;
  185. int err;
  186. size_t tries = 0;
  187. while ( (tries < LDBM_CACHE_RETRY_COUNT) &&
  188. (e = uniqueid2entry(be, uniqueid, txn, &err ))
  189. != NULL ) {
  190. /*
  191. * we'd like to return the entry. lock it if requested,
  192. * retrying if necessary.
  193. */
  194. /* wait for entry modify lock */
  195. if ( !lock || cache_lock_entry( &inst->inst_cache, e ) == 0 ) {
  196. LDAPDebug( LDAP_DEBUG_TRACE,
  197. "<= find_entry_internal_uniqueid found; uniqueid = (%s)\n",
  198. uniqueid, 0, 0 );
  199. return( e );
  200. }
  201. /*
  202. * this entry has been deleted - see if it was actually
  203. * replaced with a new copy, and try the whole thing again.
  204. */
  205. LDAPDebug( LDAP_DEBUG_ARGS,
  206. " find_entry_internal_uniqueid retrying; uniqueid = (%s)\n",
  207. uniqueid, 0, 0 );
  208. cache_return( &inst->inst_cache, &e );
  209. tries++;
  210. }
  211. if (tries >= LDBM_CACHE_RETRY_COUNT) {
  212. LDAPDebug( LDAP_DEBUG_ANY,
  213. "find_entry_internal_uniqueid retry count exceeded; uniqueid = (%s)\n",
  214. uniqueid , 0, 0 );
  215. }
  216. /* entry not found */
  217. slapi_send_ldap_result( pb, ( 0 == err || DB_NOTFOUND == err ) ?
  218. LDAP_NO_SUCH_OBJECT : LDAP_OPERATIONS_ERROR, NULL /* matched */, NULL,
  219. 0, NULL );
  220. LDAPDebug( LDAP_DEBUG_TRACE,
  221. "<= find_entry_internal not found; uniqueid = (%s)\n",
  222. uniqueid, 0, 0 );
  223. return( NULL );
  224. }
  225. static struct backentry *
  226. find_entry_internal(
  227. Slapi_PBlock *pb,
  228. Slapi_Backend *be,
  229. const entry_address *addr,
  230. int lock,
  231. back_txn *txn,
  232. int really_internal
  233. )
  234. {
  235. /* check if we should search based on uniqueid or dn */
  236. if (addr->uniqueid!=NULL)
  237. {
  238. LDAPDebug( LDAP_DEBUG_TRACE, "=> find_entry_internal (uniqueid=%s) lock %d\n",
  239. addr->uniqueid, lock, 0 );
  240. return (find_entry_internal_uniqueid (pb, be, addr->uniqueid, lock, txn));
  241. }
  242. else
  243. {
  244. Slapi_DN sdn;
  245. struct backentry *entry;
  246. slapi_sdn_init_dn_ndn_byref (&sdn, addr->dn); /* normalized by front end */
  247. LDAPDebug( LDAP_DEBUG_TRACE, "=> find_entry_internal (dn=%s) lock %d\n",
  248. addr->dn, lock, 0 );
  249. entry = find_entry_internal_dn (pb, be, &sdn, lock, txn, really_internal);
  250. slapi_sdn_done (&sdn);
  251. return entry;
  252. }
  253. }
  254. struct backentry *
  255. find_entry(
  256. Slapi_PBlock *pb,
  257. Slapi_Backend *be,
  258. const entry_address *addr,
  259. back_txn *txn
  260. )
  261. {
  262. return( find_entry_internal( pb, be, addr, 0/*!lock*/, txn, 0/*!really_internal*/ ) );
  263. }
  264. struct backentry *
  265. find_entry2modify(
  266. Slapi_PBlock *pb,
  267. Slapi_Backend *be,
  268. const entry_address *addr,
  269. back_txn *txn
  270. )
  271. {
  272. return( find_entry_internal( pb, be, addr, 1/*lock*/, txn, 0/*!really_internal*/ ) );
  273. }
  274. /* New routines which do not do any referral stuff.
  275. Call these if all you want to do is get pointer to an entry
  276. and certainly do not want any side-effects relating to client ! */
  277. struct backentry *
  278. find_entry_only(
  279. Slapi_PBlock *pb,
  280. Slapi_Backend *be,
  281. const entry_address *addr,
  282. back_txn *txn
  283. )
  284. {
  285. return( find_entry_internal( pb, be, addr, 0/*!lock*/, txn, 1/*really_internal*/ ) );
  286. }
  287. struct backentry *
  288. find_entry2modify_only(
  289. Slapi_PBlock *pb,
  290. Slapi_Backend *be,
  291. const entry_address *addr,
  292. back_txn *txn
  293. )
  294. {
  295. return( find_entry_internal( pb, be, addr, 1/*lock*/, txn, 1/*really_internal*/ ) );
  296. }