computed.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. /* Structure used to pass the context needed for completing a computed attribute operation */
  44. struct _computed_attr_context {
  45. BerElement *ber;
  46. int attrsonly;
  47. char *requested_type;
  48. Slapi_PBlock *pb;
  49. };
  50. struct _compute_evaluator {
  51. struct _compute_evaluator *next;
  52. slapi_compute_callback_t function;
  53. };
  54. typedef struct _compute_evaluator compute_evaluator;
  55. static compute_evaluator *compute_evaluators = NULL;
  56. static PRRWLock *compute_evaluators_lock = NULL;
  57. static int
  58. compute_stock_evaluator(computed_attr_context *c,char* type,Slapi_Entry *e,slapi_compute_output_t outputfn);
  59. struct _compute_rewriter {
  60. struct _compute_rewriter *next;
  61. slapi_search_rewrite_callback_t function;
  62. };
  63. typedef struct _compute_rewriter compute_rewriter;
  64. static compute_rewriter *compute_rewriters = NULL;
  65. static PRRWLock *compute_rewriters_lock = NULL;
  66. /* Function called by evaluators to have the value output */
  67. static int
  68. compute_output_callback(computed_attr_context *c,Slapi_Attr *a , Slapi_Entry *e)
  69. {
  70. return encode_attr (c->pb, c->ber, e, a, c->attrsonly, c->requested_type);
  71. }
  72. static int
  73. compute_call_evaluators(computed_attr_context *c,slapi_compute_output_t outfn,char *type,Slapi_Entry *e)
  74. {
  75. int rc = -1;
  76. compute_evaluator *current = NULL;
  77. /* Walk along the list (locked) calling the evaluator functions util one says yes, an error happens, or we finish */
  78. PR_RWLock_Rlock(compute_evaluators_lock);
  79. for (current = compute_evaluators; (current != NULL) && (-1 == rc); current = current->next) {
  80. rc = (*(current->function))(c,type,e,outfn);
  81. }
  82. PR_RWLock_Unlock(compute_evaluators_lock);
  83. return rc;
  84. }
  85. /* Returns : -1 if no attribute matched the requested type */
  86. /* 0 if one matched and it was processed without error */
  87. /* >0 if an error happened */
  88. int
  89. compute_attribute(char *type, Slapi_PBlock *pb,BerElement *ber,Slapi_Entry *e,int attrsonly,char *requested_type)
  90. {
  91. computed_attr_context context;
  92. context.ber = ber;
  93. context.attrsonly = attrsonly;
  94. context.requested_type = requested_type;
  95. context.pb = pb;
  96. return compute_call_evaluators(&context,compute_output_callback,type,e);
  97. }
  98. static int
  99. compute_stock_evaluator(computed_attr_context *c,char* type,Slapi_Entry *e,slapi_compute_output_t outputfn)
  100. {
  101. int rc= -1;
  102. static char* subschemasubentry = "subschemasubentry";
  103. if ( strcasecmp (type, subschemasubentry ) == 0)
  104. {
  105. Slapi_Attr our_attr;
  106. slapi_attr_init(&our_attr, subschemasubentry);
  107. our_attr.a_flags = SLAPI_ATTR_FLAG_OPATTR;
  108. valueset_add_string(&our_attr.a_present_values,SLAPD_SCHEMA_DN,CSN_TYPE_UNKNOWN,NULL);
  109. rc = (*outputfn) (c, &our_attr, e);
  110. attr_done(&our_attr);
  111. return (rc);
  112. }
  113. return rc; /* I see no ships */
  114. }
  115. int slapi_compute_add_evaluator(slapi_compute_callback_t function)
  116. {
  117. int rc = 0;
  118. compute_evaluator *new_eval = NULL;
  119. PR_ASSERT(NULL != function);
  120. PR_ASSERT(NULL != compute_evaluators_lock);
  121. PR_RWLock_Wlock(compute_evaluators_lock);
  122. new_eval = (compute_evaluator *)slapi_ch_calloc(1,sizeof (compute_evaluator));
  123. if (NULL == new_eval) {
  124. rc = ENOMEM;
  125. } else {
  126. new_eval->next = compute_evaluators;
  127. new_eval->function = function;
  128. compute_evaluators = new_eval;
  129. }
  130. PR_RWLock_Unlock(compute_evaluators_lock);
  131. return rc;
  132. }
  133. /* Call this on server startup, before the first LDAP operation is serviced */
  134. int compute_init()
  135. {
  136. /* Initialize the lock */
  137. compute_evaluators_lock = PR_NewRWLock( PR_RWLOCK_RANK_NONE, "compute_attr_lock" );
  138. if (NULL == compute_evaluators_lock) {
  139. /* Out of resources */
  140. return ENOMEM;
  141. }
  142. compute_rewriters_lock = PR_NewRWLock( PR_RWLOCK_RANK_NONE, "compute_rewriters_lock" );
  143. if (NULL == compute_rewriters_lock) {
  144. /* Out of resources */
  145. return ENOMEM;
  146. }
  147. /* Now add the stock evaluators to the list */
  148. return slapi_compute_add_evaluator(compute_stock_evaluator);
  149. }
  150. /* Call this on server shutdown, after the last LDAP operation has
  151. terminated */
  152. int compute_terminate()
  153. {
  154. /* Free the list */
  155. if (NULL != compute_evaluators_lock) {
  156. compute_evaluator *current = compute_evaluators;
  157. PR_RWLock_Wlock(compute_evaluators_lock);
  158. while (current != NULL) {
  159. compute_evaluator *asabird = current;
  160. current = current->next;
  161. slapi_ch_free((void **)&asabird);
  162. }
  163. PR_RWLock_Unlock(compute_evaluators_lock);
  164. /* Free the lock */
  165. PR_DestroyRWLock(compute_evaluators_lock);
  166. }
  167. if (NULL != compute_rewriters_lock) {
  168. compute_rewriter *current = compute_rewriters;
  169. PR_RWLock_Wlock(compute_rewriters_lock);
  170. while (current != NULL) {
  171. compute_rewriter *asabird = current;
  172. current = current->next;
  173. slapi_ch_free((void **)&asabird);
  174. }
  175. PR_RWLock_Unlock(compute_rewriters_lock);
  176. PR_DestroyRWLock(compute_rewriters_lock);
  177. }
  178. return 0;
  179. }
  180. /* Functions dealing with re-writing of search filters */
  181. int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function)
  182. {
  183. int rc = 0;
  184. compute_rewriter *new_rewriter = NULL;
  185. PR_ASSERT(NULL != function);
  186. PR_ASSERT(NULL != compute_rewriters_lock);
  187. new_rewriter = (compute_rewriter *)slapi_ch_calloc(1,sizeof (compute_rewriter));
  188. if (NULL == new_rewriter) {
  189. rc = ENOMEM;
  190. } else {
  191. PR_RWLock_Wlock(compute_rewriters_lock);
  192. new_rewriter->next = compute_rewriters;
  193. new_rewriter->function = function;
  194. compute_rewriters = new_rewriter;
  195. PR_RWLock_Unlock(compute_rewriters_lock);
  196. }
  197. return rc;
  198. }
  199. int compute_rewrite_search_filter(Slapi_PBlock *pb)
  200. {
  201. /* Iterate through the listed rewriters until one says it matched */
  202. int rc = -1;
  203. compute_rewriter *current = NULL;
  204. /* Walk along the list (locked) calling the evaluator functions util one says yes, an error happens, or we finish */
  205. PR_RWLock_Rlock(compute_rewriters_lock);
  206. for (current = compute_rewriters; (current != NULL) && (-1 == rc); current = current->next) {
  207. rc = (*(current->function))(pb);
  208. /* Meaning of the return code :
  209. -1 : keep looking
  210. 0 : rewrote OK
  211. 1 : refuse to do this search
  212. 2 : operations error
  213. */
  214. }
  215. PR_RWLock_Unlock(compute_rewriters_lock);
  216. return rc;
  217. }