computed.c 8.0 KB

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