plugin_acl.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. * plugin_acl.c - routines for calling access control plugins
  43. */
  44. #include "slap.h"
  45. static int
  46. acl_default_access ( Slapi_PBlock *pb , Slapi_Entry *e, int access)
  47. {
  48. int isRoot, rootdse, accessCheckDisabled;
  49. int rc;
  50. slapi_pblock_get ( pb, SLAPI_REQUESTOR_ISROOT, &isRoot);
  51. if ( isRoot ) return LDAP_SUCCESS;
  52. rc = slapi_pblock_get ( pb, SLAPI_PLUGIN_DB_NO_ACL, &accessCheckDisabled );
  53. if ( rc != -1 && accessCheckDisabled ) return LDAP_SUCCESS;
  54. rootdse = slapi_is_rootdse ( slapi_entry_get_ndn ( e ) );
  55. if ( rootdse && (access & (SLAPI_ACL_READ | SLAPI_ACL_SEARCH) ) )
  56. return LDAP_SUCCESS;
  57. return LDAP_INSUFFICIENT_ACCESS;
  58. }
  59. int
  60. plugin_call_acl_plugin ( Slapi_PBlock *pb, Slapi_Entry *e, char **attrs,
  61. struct berval *val, int access , int flags, char **errbuf)
  62. {
  63. struct slapdplugin *p;
  64. int rc = LDAP_INSUFFICIENT_ACCESS;
  65. int aclplugin_initialized = 0;
  66. Operation *operation;
  67. slapi_pblock_get (pb, SLAPI_OPERATION, &operation);
  68. /* we don't perform acl check for internal operations and if the plugin has set it not to be checked */
  69. if (operation_is_flag_set(operation, SLAPI_OP_FLAG_NO_ACCESS_CHECK|OP_FLAG_INTERNAL|OP_FLAG_REPLICATED|OP_FLAG_LEGACY_REPLICATION_DN))
  70. return LDAP_SUCCESS;
  71. /* call the global plugins first and then the backend specific */
  72. for ( p = get_plugin_list(PLUGIN_LIST_ACL); p != NULL; p = p->plg_next ) {
  73. if (plugin_invoke_plugin_sdn (p, SLAPI_PLUGIN_ACL_ALLOW_ACCESS, pb,
  74. (Slapi_DN*)slapi_entry_get_sdn_const (e))){
  75. aclplugin_initialized = 1;
  76. rc = (*p->plg_acl_access_allowed)(pb, e, attrs, val, access, flags, errbuf);
  77. if ( rc != LDAP_SUCCESS ) break;
  78. }
  79. }
  80. if (! aclplugin_initialized ) {
  81. rc = acl_default_access ( pb, e, access);
  82. }
  83. return rc;
  84. }
  85. int
  86. plugin_call_acl_mods_access ( Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf )
  87. {
  88. struct slapdplugin *p;
  89. int aclplugin_initialized = 0;
  90. int rc = LDAP_INSUFFICIENT_ACCESS;
  91. Operation *operation;
  92. slapi_pblock_get (pb, SLAPI_OPERATION, &operation);
  93. /* we don't perform acl check for internal operations and if the plugin has set it not to be checked */
  94. if (operation_is_flag_set(operation, SLAPI_OP_FLAG_NO_ACCESS_CHECK|OP_FLAG_INTERNAL|OP_FLAG_REPLICATED|OP_FLAG_LEGACY_REPLICATION_DN))
  95. return LDAP_SUCCESS;
  96. /* call the global plugins first and then the backend specific */
  97. for ( p = get_plugin_list(PLUGIN_LIST_ACL); p != NULL; p = p->plg_next ) {
  98. if (plugin_invoke_plugin_sdn (p, SLAPI_PLUGIN_ACL_MODS_ALLOWED, pb,
  99. (Slapi_DN*)slapi_entry_get_sdn_const (e))){
  100. aclplugin_initialized = 1;
  101. rc = (*p->plg_acl_mods_allowed)( pb, e, mods, errbuf );
  102. if ( rc != LDAP_SUCCESS ) break;
  103. }
  104. }
  105. if (! aclplugin_initialized ) {
  106. rc = acl_default_access ( pb, e, SLAPI_ACL_WRITE);
  107. }
  108. return rc;
  109. }
  110. /* This plugin should be called immediatly after the changes have been comitted */
  111. /* This function is now fully executed for internal and replicated ops. */
  112. int
  113. plugin_call_acl_mods_update ( Slapi_PBlock *pb, int optype )
  114. {
  115. struct slapdplugin *p;
  116. char *dn;
  117. int rc = 0;
  118. void *change = NULL;
  119. Slapi_Entry *te = NULL;
  120. Slapi_DN sdn;
  121. Operation *operation;
  122. slapi_pblock_get (pb, SLAPI_OPERATION, &operation);
  123. (void)slapi_pblock_get( pb, SLAPI_TARGET_DN, &dn );
  124. switch ( optype ) {
  125. case SLAPI_OPERATION_MODIFY:
  126. (void)slapi_pblock_get( pb, SLAPI_MODIFY_MODS, &change );
  127. break;
  128. case SLAPI_OPERATION_ADD:
  129. (void)slapi_pblock_get( pb, SLAPI_ADD_ENTRY, &change );
  130. te = (Slapi_Entry *)change;
  131. if(!slapi_sdn_isempty(slapi_entry_get_sdn(te)))
  132. {
  133. dn= (char*)slapi_sdn_get_ndn(slapi_entry_get_sdn(te)); /* jcm - Had to cast away const */
  134. }
  135. break;
  136. case SLAPI_OPERATION_MODRDN:
  137. (void)slapi_pblock_get( pb, SLAPI_MODRDN_NEWRDN, &change );
  138. break;
  139. }
  140. slapi_sdn_init_dn_byref (&sdn, dn);
  141. /* call the global plugins first and then the backend specific */
  142. for ( p = get_plugin_list(PLUGIN_LIST_ACL); p != NULL; p = p->plg_next ) {
  143. if (plugin_invoke_plugin_sdn (p, SLAPI_PLUGIN_ACL_MODS_UPDATE, pb, &sdn)){
  144. rc = (*p->plg_acl_mods_update)(pb, optype, dn, change );
  145. if ( rc != LDAP_SUCCESS ) break;
  146. }
  147. }
  148. slapi_sdn_done (&sdn);
  149. return rc;
  150. }
  151. int
  152. plugin_call_acl_verify_syntax ( Slapi_PBlock *pb, Slapi_Entry *e, char **errbuf )
  153. {
  154. struct slapdplugin *p;
  155. int rc = 0;
  156. int plugin_called = 0;
  157. Operation *operation;
  158. slapi_pblock_get (pb, SLAPI_OPERATION, &operation);
  159. /* we don't perform acl check for internal operations and if the plugin has set it not to be checked */
  160. if (operation_is_flag_set(operation, SLAPI_OP_FLAG_NO_ACCESS_CHECK|OP_FLAG_INTERNAL|OP_FLAG_REPLICATED|OP_FLAG_LEGACY_REPLICATION_DN))
  161. return LDAP_SUCCESS;
  162. /* call the global plugins first and then the backend specific */
  163. for ( p = get_plugin_list(PLUGIN_LIST_ACL); p != NULL; p = p->plg_next ) {
  164. if (plugin_invoke_plugin_sdn (p, SLAPI_PLUGIN_ACL_SYNTAX_CHECK, pb,
  165. (Slapi_DN*)slapi_entry_get_sdn_const (e))){
  166. plugin_called = 1;
  167. rc = (*p->plg_acl_syntax_check)( e, errbuf );
  168. if ( rc != LDAP_SUCCESS ) break;
  169. }
  170. }
  171. if ( !plugin_called ) {
  172. LDAPDebug ( LDAP_DEBUG_ANY, "The ACL plugin is not initialized. The aci syntax cannot be verified\n",0,0,0);
  173. }
  174. return rc;
  175. }
  176. int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
  177. struct berval *val, int access )
  178. {
  179. char *attrs[2] = { NULL, NULL };
  180. attrs[0] = attr;
  181. return ( plugin_call_acl_plugin ( pb, e, attrs, val, access, ACLPLUGIN_ACCESS_DEFAULT, NULL ) );
  182. }
  183. int slapi_acl_check_mods( Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf )
  184. {
  185. return ( plugin_call_acl_mods_access ( pb, e, mods, errbuf ) );
  186. }
  187. int slapi_acl_verify_aci_syntax (Slapi_PBlock *pb, Slapi_Entry *e, char **errbuf)
  188. {
  189. return ( plugin_call_acl_verify_syntax ( pb, e, errbuf ) );
  190. }