utils.c 6.8 KB

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