utils.c 4.9 KB

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