utils.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. /***********************************************************************
  42. ** NAME
  43. ** utils.c
  44. **
  45. ** DESCRIPTION
  46. **
  47. **
  48. ** AUTHOR
  49. ** <[email protected]>
  50. **
  51. ***********************************************************************/
  52. /***********************************************************************
  53. ** Includes
  54. ***********************************************************************/
  55. #include "plugin-utils.h"
  56. #include "nspr.h"
  57. static char *plugin_name = "utils";
  58. /*
  59. * Lock for updating a counter (global for all counters)
  60. */
  61. static Slapi_Mutex *counter_lock = NULL;
  62. /* ------------------------------------------------------------ */
  63. /*
  64. * op_error - Record (and report) an operational error.
  65. */
  66. int
  67. op_error(int internal_error) {
  68. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  69. "Internal error: %d\n", internal_error);
  70. return LDAP_OPERATIONS_ERROR;
  71. }
  72. /* ------------------------------------------------------------ */
  73. /*
  74. * readPblockAndEntry - search for and read an entry
  75. * Return:
  76. * A pblock containing the entry, or NULL
  77. */
  78. Slapi_PBlock *
  79. readPblockAndEntry( const char *baseDN, const char *filter,
  80. char *attrs[] ) {
  81. Slapi_PBlock *spb = NULL;
  82. BEGIN
  83. int sres;
  84. /* Perform the search - the new pblock needs to be freed */
  85. spb = slapi_search_internal((char *)baseDN, LDAP_SCOPE_BASE,
  86. (char *)filter, NULL, attrs, 0);
  87. if ( !spb ) {
  88. op_error(20);
  89. break;
  90. }
  91. if ( slapi_pblock_get( spb, SLAPI_PLUGIN_INTOP_RESULT, &sres ) ) {
  92. op_error(21);
  93. break;
  94. } else if (sres) {
  95. op_error(22);
  96. break;
  97. }
  98. END
  99. return spb;
  100. }
  101. /* ------------------------------------------------------------ */
  102. /*
  103. * hasObjectClass - read an entry and check if it has a
  104. * particular object class value
  105. * Return:
  106. * 1 - the entry contains the object class value
  107. * 0 - the entry doesn't contain the object class value
  108. */
  109. int
  110. entryHasObjectClass(Slapi_PBlock *pb, Slapi_Entry *e,
  111. const char *objectClass) {
  112. Slapi_Attr *attr;
  113. Slapi_Value *v;
  114. const struct berval *bv;
  115. int vhint;
  116. if ( slapi_entry_attr_find(e, "objectclass", &attr) ) {
  117. return 0; /* no objectclass values! */
  118. }
  119. /*
  120. * Check each of the object class values in turn.
  121. */
  122. for ( vhint = slapi_attr_first_value( attr, &v );
  123. vhint != -1;
  124. vhint = slapi_attr_next_value( attr, vhint, &v )) {
  125. bv = slapi_value_get_berval(v);
  126. if ( NULL != bv && NULL != bv->bv_val &&
  127. !strcasecmp(bv->bv_val, objectClass) ) {
  128. return 1;
  129. }
  130. }
  131. return 0;
  132. }
  133. /* ------------------------------------------------------------ */
  134. /*
  135. * dnHasObjectClass - read an entry if it has a particular object class
  136. * Return:
  137. * A pblock containing the entry, or NULL
  138. */
  139. Slapi_PBlock *
  140. dnHasObjectClass( const char *baseDN, const char *objectClass ) {
  141. char *filter = NULL;
  142. Slapi_PBlock *spb = NULL;
  143. BEGIN
  144. Slapi_Entry **entries;
  145. char *attrs[2];
  146. /* Perform the search - the new pblock needs to be freed */
  147. attrs[0] = "objectclass";
  148. attrs[1] = NULL;
  149. filter = PR_smprintf("objectclass=%s", objectClass );
  150. if ( !(spb = readPblockAndEntry( baseDN, filter, attrs) ) ) {
  151. break;
  152. }
  153. if ( slapi_pblock_get(spb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  154. &entries) ) {
  155. op_error(23);
  156. break;
  157. }
  158. /*
  159. * Can only be one entry returned on a base search; just check
  160. * the first one
  161. */
  162. if ( !*entries ) {
  163. /* Clean up */
  164. slapi_free_search_results_internal(spb);
  165. slapi_pblock_destroy(spb);
  166. spb = NULL;
  167. }
  168. END
  169. if (filter) {
  170. PR_smprintf_free(filter);
  171. }
  172. return spb;
  173. }
  174. /* ------------------------------------------------------------ */
  175. /*
  176. * dnHasAttribute - read an entry if it has a particular attribute
  177. * Return:
  178. * The entry, or NULL
  179. */
  180. Slapi_PBlock *
  181. dnHasAttribute( const char *baseDN, const char *attrName ) {
  182. Slapi_PBlock *spb = NULL;
  183. char *filter = NULL;
  184. BEGIN
  185. int sres;
  186. Slapi_Entry **entries;
  187. char *attrs[2];
  188. /* Perform the search - the new pblock needs to be freed */
  189. attrs[0] = (char *)attrName;
  190. attrs[1] = NULL;
  191. filter = PR_smprintf( "%s=*", attrName );
  192. spb = slapi_search_internal((char *)baseDN, LDAP_SCOPE_BASE,
  193. filter, NULL, attrs, 0);
  194. if ( !spb ) {
  195. op_error(20);
  196. break;
  197. }
  198. if ( slapi_pblock_get( spb, SLAPI_PLUGIN_INTOP_RESULT, &sres ) ) {
  199. op_error(21);
  200. break;
  201. } else if (sres) {
  202. op_error(22);
  203. break;
  204. }
  205. if ( slapi_pblock_get(spb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  206. &entries) ) {
  207. op_error(23);
  208. break;
  209. }
  210. /*
  211. * Can only be one entry returned on a base search; just check
  212. * the first one
  213. */
  214. if ( !*entries ) {
  215. /* Clean up */
  216. slapi_free_search_results_internal(spb);
  217. slapi_pblock_destroy(spb);
  218. spb = NULL;
  219. }
  220. END
  221. if (filter) {
  222. PR_smprintf_free(filter);
  223. }
  224. return spb;
  225. }