utils.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. #include <config.h>
  11. #endif
  12. /***********************************************************************
  13. ** NAME
  14. ** utils.c
  15. **
  16. ** DESCRIPTION
  17. **
  18. **
  19. ** AUTHOR
  20. ** <[email protected]>
  21. **
  22. ***********************************************************************/
  23. /***********************************************************************
  24. ** Includes
  25. ***********************************************************************/
  26. #include "plugin-utils.h"
  27. #include "nspr.h"
  28. static char *plugin_name = "utils";
  29. /* ------------------------------------------------------------ */
  30. /*
  31. * op_error - Record (and report) an operational error.
  32. */
  33. int
  34. op_error(int internal_error)
  35. {
  36. slapi_log_err(SLAPI_LOG_PLUGIN, plugin_name,
  37. "Internal error: %d\n", internal_error);
  38. return LDAP_OPERATIONS_ERROR;
  39. }
  40. /* ------------------------------------------------------------ */
  41. /*
  42. * readPblockAndEntry - search for and read an entry
  43. * Return:
  44. * A pblock containing the entry, or NULL
  45. */
  46. Slapi_PBlock *
  47. readPblockAndEntry(Slapi_DN *baseDN, const char *filter, char *attrs[])
  48. {
  49. Slapi_PBlock *spb = NULL;
  50. BEGIN
  51. int sres;
  52. /* Perform the search - the new pblock needs to be freed */
  53. spb = slapi_search_internal(slapi_sdn_get_dn(baseDN), LDAP_SCOPE_BASE,
  54. (char *)filter, NULL, attrs, 0);
  55. if (!spb) {
  56. op_error(20);
  57. break;
  58. }
  59. if (slapi_pblock_get(spb, SLAPI_PLUGIN_INTOP_RESULT, &sres)) {
  60. op_error(21);
  61. break;
  62. } else if (sres) {
  63. op_error(22);
  64. break;
  65. }
  66. END
  67. return spb;
  68. }
  69. /* ------------------------------------------------------------ */
  70. /*
  71. * hasObjectClass - read an entry and check if it has a
  72. * particular object class value
  73. * Return:
  74. * 1 - the entry contains the object class value
  75. * 0 - the entry doesn't contain the object class value
  76. */
  77. int
  78. entryHasObjectClass(Slapi_PBlock *pb __attribute__((unused)), Slapi_Entry *e, const char *objectClass)
  79. {
  80. Slapi_Attr *attr;
  81. Slapi_Value *v;
  82. const struct berval *bv;
  83. int vhint;
  84. if (slapi_entry_attr_find(e, "objectclass", &attr)) {
  85. return 0; /* no objectclass values! */
  86. }
  87. /*
  88. * Check each of the object class values in turn.
  89. */
  90. for (vhint = slapi_attr_first_value(attr, &v);
  91. vhint != -1;
  92. vhint = slapi_attr_next_value(attr, vhint, &v)) {
  93. bv = slapi_value_get_berval(v);
  94. if (NULL != bv && NULL != bv->bv_val &&
  95. !strcasecmp(bv->bv_val, objectClass)) {
  96. return 1;
  97. }
  98. }
  99. return 0;
  100. }
  101. /* ------------------------------------------------------------ */
  102. /*
  103. * dnHasObjectClass - read an entry if it has a particular object class
  104. * Return:
  105. * A pblock containing the entry, or NULL
  106. */
  107. Slapi_PBlock *
  108. dnHasObjectClass(Slapi_DN *baseDN, const char *objectClass)
  109. {
  110. char *filter = NULL;
  111. Slapi_PBlock *spb = NULL;
  112. BEGIN
  113. Slapi_Entry **entries;
  114. char *attrs[2];
  115. /* Perform the search - the new pblock needs to be freed */
  116. attrs[0] = "objectclass";
  117. attrs[1] = NULL;
  118. filter = PR_smprintf("objectclass=%s", objectClass);
  119. if (!(spb = readPblockAndEntry(baseDN, filter, attrs))) {
  120. break;
  121. }
  122. if (slapi_pblock_get(spb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  123. &entries)) {
  124. op_error(23);
  125. break;
  126. }
  127. /*
  128. * Can only be one entry returned on a base search; just check
  129. * the first one
  130. */
  131. if (!*entries) {
  132. /* Clean up */
  133. slapi_free_search_results_internal(spb);
  134. slapi_pblock_destroy(spb);
  135. spb = NULL;
  136. }
  137. END
  138. if (filter)
  139. {
  140. PR_smprintf_free(filter);
  141. }
  142. return spb;
  143. }
  144. /* ------------------------------------------------------------ */
  145. /*
  146. * dnHasAttribute - read an entry if it has a particular attribute
  147. * Return:
  148. * The entry, or NULL
  149. */
  150. Slapi_PBlock *
  151. dnHasAttribute(const char *baseDN, const char *attrName)
  152. {
  153. Slapi_PBlock *spb = NULL;
  154. char *filter = NULL;
  155. BEGIN
  156. int sres;
  157. Slapi_Entry **entries;
  158. char *attrs[2];
  159. /* Perform the search - the new pblock needs to be freed */
  160. attrs[0] = (char *)attrName;
  161. attrs[1] = NULL;
  162. filter = PR_smprintf("%s=*", attrName);
  163. spb = slapi_search_internal((char *)baseDN, LDAP_SCOPE_BASE,
  164. filter, NULL, attrs, 0);
  165. if (!spb) {
  166. op_error(20);
  167. break;
  168. }
  169. if (slapi_pblock_get(spb, SLAPI_PLUGIN_INTOP_RESULT, &sres)) {
  170. op_error(21);
  171. break;
  172. } else if (sres) {
  173. op_error(22);
  174. break;
  175. }
  176. if (slapi_pblock_get(spb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  177. &entries)) {
  178. op_error(23);
  179. break;
  180. }
  181. /*
  182. * Can only be one entry returned on a base search; just check
  183. * the first one
  184. */
  185. if (!*entries) {
  186. /* Clean up */
  187. slapi_free_search_results_internal(spb);
  188. slapi_pblock_destroy(spb);
  189. spb = NULL;
  190. }
  191. END
  192. if (filter)
  193. {
  194. PR_smprintf_free(filter);
  195. }
  196. return spb;
  197. }