computed.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. /* Handles computed attributes for entries as they're returned to the client */
  42. #include "slap.h"
  43. #include "proto-slap.h"
  44. /* Structure used to pass the context needed for completing a computed attribute operation */
  45. struct _computed_attr_context {
  46. BerElement *ber;
  47. int attrsonly;
  48. char *requested_type;
  49. Slapi_PBlock *pb;
  50. };
  51. struct _compute_evaluator {
  52. struct _compute_evaluator *next;
  53. slapi_compute_callback_t function;
  54. };
  55. typedef struct _compute_evaluator compute_evaluator;
  56. static PRBool startup_completed = PR_FALSE;
  57. static compute_evaluator *compute_evaluators = NULL;
  58. static Slapi_RWLock *compute_evaluators_lock = NULL;
  59. static PRBool require_compute_evaluator_lock = PR_FALSE;
  60. static int
  61. compute_stock_evaluator(computed_attr_context *c,char* type,Slapi_Entry *e,slapi_compute_output_t outputfn);
  62. struct _compute_rewriter {
  63. struct _compute_rewriter *next;
  64. slapi_search_rewrite_callback_t function;
  65. };
  66. typedef struct _compute_rewriter compute_rewriter;
  67. static compute_rewriter *compute_rewriters = NULL;
  68. static Slapi_RWLock *compute_rewriters_lock = NULL;
  69. static PRBool require_compute_rewriters_lock = PR_FALSE;
  70. /* Function called by evaluators to have the value output */
  71. static int
  72. compute_output_callback(computed_attr_context *c,Slapi_Attr *a , Slapi_Entry *e)
  73. {
  74. return encode_attr (c->pb, c->ber, e, a, c->attrsonly, c->requested_type);
  75. }
  76. static
  77. int compute_call_evaluators_nolock(computed_attr_context *c,slapi_compute_output_t outfn,char *type,Slapi_Entry *e)
  78. {
  79. int rc = -1;
  80. compute_evaluator *current = NULL;
  81. for (current = compute_evaluators; (current != NULL) && (-1 == rc); current = current->next) {
  82. rc = (*(current->function))(c,type,e,outfn);
  83. }
  84. return rc;
  85. }
  86. static int
  87. compute_call_evaluators(computed_attr_context *c,slapi_compute_output_t outfn,char *type,Slapi_Entry *e)
  88. {
  89. int rc = -1;
  90. int need_lock = require_compute_evaluator_lock;
  91. /* Walk along the list (locked) calling the evaluator functions util one says yes, an error happens, or we finish */
  92. if (need_lock) {
  93. slapi_rwlock_rdlock(compute_evaluators_lock);
  94. }
  95. rc = compute_call_evaluators_nolock(c, outfn, type, e);
  96. if (need_lock) {
  97. slapi_rwlock_unlock(compute_evaluators_lock);
  98. }
  99. return rc;
  100. }
  101. /* Returns : -1 if no attribute matched the requested type */
  102. /* 0 if one matched and it was processed without error */
  103. /* >0 if an error happened */
  104. int
  105. compute_attribute(char *type, Slapi_PBlock *pb,BerElement *ber,Slapi_Entry *e,int attrsonly,char *requested_type)
  106. {
  107. computed_attr_context context;
  108. context.ber = ber;
  109. context.attrsonly = attrsonly;
  110. context.requested_type = requested_type;
  111. context.pb = pb;
  112. return compute_call_evaluators(&context,compute_output_callback,type,e);
  113. }
  114. static int
  115. compute_stock_evaluator(computed_attr_context *c,char* type,Slapi_Entry *e,slapi_compute_output_t outputfn)
  116. {
  117. int rc= -1;
  118. static char* subschemasubentry = "subschemasubentry";
  119. if ( strcasecmp (type, subschemasubentry ) == 0)
  120. {
  121. Slapi_Attr our_attr;
  122. slapi_attr_init(&our_attr, subschemasubentry);
  123. our_attr.a_flags = SLAPI_ATTR_FLAG_OPATTR;
  124. valueset_add_string(&our_attr, &our_attr.a_present_values,SLAPD_SCHEMA_DN,CSN_TYPE_UNKNOWN,NULL);
  125. rc = (*outputfn) (c, &our_attr, e);
  126. attr_done(&our_attr);
  127. return (rc);
  128. }
  129. return rc; /* I see no ships */
  130. }
  131. static void
  132. compute_add_evaluator_nolock(slapi_compute_callback_t function, compute_evaluator *new_eval)
  133. {
  134. new_eval->next = compute_evaluators;
  135. new_eval->function = function;
  136. compute_evaluators = new_eval;
  137. }
  138. int slapi_compute_add_evaluator(slapi_compute_callback_t function)
  139. {
  140. int rc = 0;
  141. compute_evaluator *new_eval = NULL;
  142. PR_ASSERT(NULL != function);
  143. PR_ASSERT(NULL != compute_evaluators_lock);
  144. if (startup_completed) {
  145. /* We are now in multi-threaded and we still add
  146. * a attribute evaluator.
  147. * switch to use locking mechanimsm
  148. */
  149. require_compute_evaluator_lock = PR_TRUE;
  150. }
  151. new_eval = (compute_evaluator *)slapi_ch_calloc(1,sizeof (compute_evaluator));
  152. if (NULL == new_eval) {
  153. rc = ENOMEM;
  154. } else {
  155. int need_lock = require_compute_evaluator_lock;
  156. if (need_lock) {
  157. slapi_rwlock_wrlock(compute_evaluators_lock);
  158. }
  159. compute_add_evaluator_nolock(function, new_eval);
  160. if (need_lock) {
  161. slapi_rwlock_unlock(compute_evaluators_lock);
  162. }
  163. }
  164. return rc;
  165. }
  166. /* Called when */
  167. void
  168. compute_plugins_started()
  169. {
  170. startup_completed = PR_TRUE;
  171. }
  172. /* Call this on server startup, before the first LDAP operation is serviced */
  173. int compute_init()
  174. {
  175. /* Initialize the lock */
  176. compute_evaluators_lock = slapi_new_rwlock();
  177. if (NULL == compute_evaluators_lock) {
  178. /* Out of resources */
  179. return ENOMEM;
  180. }
  181. compute_rewriters_lock = slapi_new_rwlock();
  182. if (NULL == compute_rewriters_lock) {
  183. /* Out of resources */
  184. return ENOMEM;
  185. }
  186. /* Now add the stock evaluators to the list */
  187. return slapi_compute_add_evaluator(compute_stock_evaluator);
  188. }
  189. /* Call this on server shutdown, after the last LDAP operation has
  190. terminated */
  191. int compute_terminate()
  192. {
  193. /* Free the list */
  194. if (NULL != compute_evaluators_lock) {
  195. compute_evaluator *current = compute_evaluators;
  196. slapi_rwlock_wrlock(compute_evaluators_lock);
  197. while (current != NULL) {
  198. compute_evaluator *asabird = current;
  199. current = current->next;
  200. slapi_ch_free((void **)&asabird);
  201. }
  202. slapi_rwlock_unlock(compute_evaluators_lock);
  203. /* Free the lock */
  204. slapi_destroy_rwlock(compute_evaluators_lock);
  205. }
  206. if (NULL != compute_rewriters_lock) {
  207. compute_rewriter *current = compute_rewriters;
  208. slapi_rwlock_wrlock(compute_rewriters_lock);
  209. while (current != NULL) {
  210. compute_rewriter *asabird = current;
  211. current = current->next;
  212. slapi_ch_free((void **)&asabird);
  213. }
  214. slapi_rwlock_unlock(compute_rewriters_lock);
  215. slapi_destroy_rwlock(compute_rewriters_lock);
  216. }
  217. return 0;
  218. }
  219. static void
  220. compute_add_search_rewrite_nolock(slapi_search_rewrite_callback_t function, compute_rewriter *new_rewriter)
  221. {
  222. new_rewriter->next = compute_rewriters;
  223. new_rewriter->function = function;
  224. compute_rewriters = new_rewriter;
  225. }
  226. /* Functions dealing with re-writing of search filters */
  227. int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function)
  228. {
  229. int rc = 0;
  230. compute_rewriter *new_rewriter = NULL;
  231. PR_ASSERT(NULL != function);
  232. PR_ASSERT(NULL != compute_rewriters_lock);
  233. if (startup_completed) {
  234. /* We are now in multi-threaded and we still add
  235. * a filter rewriter.
  236. * switch to use locking mechanimsm
  237. */
  238. require_compute_rewriters_lock = PR_TRUE;
  239. }
  240. new_rewriter = (compute_rewriter *)slapi_ch_calloc(1,sizeof (compute_rewriter));
  241. if (NULL == new_rewriter) {
  242. rc = ENOMEM;
  243. } else {
  244. int need_lock = require_compute_rewriters_lock;
  245. if (need_lock) {
  246. slapi_rwlock_wrlock(compute_rewriters_lock);
  247. }
  248. compute_add_search_rewrite_nolock(function, new_rewriter);
  249. if (need_lock) {
  250. slapi_rwlock_unlock(compute_rewriters_lock);
  251. }
  252. }
  253. return rc;
  254. }
  255. static
  256. int compute_rewrite_search_filter_nolock(Slapi_PBlock *pb)
  257. {
  258. int rc = -1;
  259. compute_rewriter *current = NULL;
  260. for (current = compute_rewriters; (current != NULL) && (-1 == rc); current = current->next) {
  261. rc = (*(current->function))(pb);
  262. /* Meaning of the return code :
  263. -1 : keep looking
  264. 0 : rewrote OK
  265. 1 : refuse to do this search
  266. 2 : operations error
  267. */
  268. }
  269. return(rc);
  270. }
  271. int compute_rewrite_search_filter(Slapi_PBlock *pb)
  272. {
  273. /* Iterate through the listed rewriters until one says it matched */
  274. int rc = -1;
  275. int need_lock = require_compute_rewriters_lock;
  276. /* Walk along the list (locked) calling the evaluator functions util one says yes, an error happens, or we finish */
  277. if (need_lock) {
  278. slapi_rwlock_rdlock(compute_rewriters_lock);
  279. }
  280. rc = compute_rewrite_search_filter_nolock(pb);
  281. if (need_lock) {
  282. slapi_rwlock_unlock(compute_rewriters_lock);
  283. }
  284. return rc;
  285. }