acct_init.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /******************************************************************************
  2. Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. version 2 as published by the Free Software Foundation.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  13. Contributors:
  14. Hewlett-Packard Development Company, L.P.
  15. ******************************************************************************/
  16. /* Example enabling and config entries
  17. dn: cn=Account Policy Plugin,cn=plugins,cn=config
  18. objectClass: top
  19. objectClass: nsSlapdPlugin
  20. objectClass: extensibleObject
  21. cn: Account Policy Plugin
  22. nsslapd-pluginPath: /path/to/libacctpolicy-plugin.sl
  23. nsslapd-pluginInitfunc: acct_policy_init
  24. nsslapd-pluginType: object
  25. nsslapd-pluginEnabled: on
  26. nsslapd-plugin-depends-on-type: database
  27. nsslapd-pluginId: Account Policy Plugin
  28. dn: cn=config,cn=Account Policy Plugin,cn=plugins,cn=config
  29. objectClass: top
  30. objectClass: extensibleObject
  31. cn: config
  32. alwaysrecordlogin: yes
  33. stateattrname: lastLoginTime
  34. altstateattrname: createTimestamp
  35. specattrname: acctPolicySubentry
  36. limitattrname: accountInactivityLimit
  37. */
  38. #include <stdio.h>
  39. #include <string.h>
  40. #include "slapi-plugin.h"
  41. #include "acctpolicy.h"
  42. static Slapi_PluginDesc plugin_desc = { PLUGIN_NAME, PLUGIN_VENDOR,
  43. PLUGIN_VERSION, PLUGIN_DESC };
  44. static Slapi_PluginDesc pre_plugin_desc = { PRE_PLUGIN_NAME, PLUGIN_VENDOR,
  45. PLUGIN_VERSION, PLUGIN_DESC };
  46. static Slapi_PluginDesc post_plugin_desc = { PRE_PLUGIN_NAME, PLUGIN_VENDOR,
  47. PLUGIN_VERSION, PLUGIN_DESC };
  48. /* Local function prototypes */
  49. int acct_policy_start( Slapi_PBlock *pb );
  50. int acct_policy_close( Slapi_PBlock *pb );
  51. int acct_policy_init( Slapi_PBlock *pb );
  52. int acct_preop_init( Slapi_PBlock *pb );
  53. int acct_postop_init( Slapi_PBlock *pb );
  54. int acct_bind_preop( Slapi_PBlock *pb );
  55. int acct_bind_postop( Slapi_PBlock *pb );
  56. /*
  57. Master init function for the account plugin
  58. */
  59. int
  60. acct_policy_init( Slapi_PBlock *pb )
  61. {
  62. void *plugin_id;
  63. int enabled;
  64. slapi_pblock_get(pb, SLAPI_PLUGIN_ENABLED, &enabled);
  65. if (!enabled) {
  66. /* not enabled */
  67. return( CALLBACK_OK );
  68. }
  69. if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  70. SLAPI_PLUGIN_VERSION_01 ) != 0 ||
  71. slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  72. (void *)&plugin_desc ) != 0 ||
  73. slapi_pblock_set( pb, SLAPI_PLUGIN_CLOSE_FN,
  74. (void *)&acct_policy_close ) != 0 ||
  75. slapi_pblock_set( pb, SLAPI_PLUGIN_START_FN,
  76. (void *)acct_policy_start ) != 0 ) {
  77. slapi_log_error( SLAPI_LOG_FATAL, PLUGIN_NAME,
  78. "acct_policy_init registration failed\n" );
  79. return( CALLBACK_ERR );
  80. }
  81. if( slapi_pblock_get( pb, SLAPI_PLUGIN_IDENTITY, &plugin_id ) != 0 ) {
  82. slapi_log_error( SLAPI_LOG_FATAL, PLUGIN_NAME,
  83. "acct_policy_init failed to get plugin identity\n" );
  84. return( CALLBACK_ERR );
  85. }
  86. set_identity( plugin_id );
  87. /* Register the pre and postop plugins */
  88. if( slapi_register_plugin("preoperation", 1, "acct_preop_init",
  89. acct_preop_init, PRE_PLUGIN_DESC, NULL, plugin_id) != 0 ||
  90. slapi_register_plugin("postoperation", 1, "acct_postop_init",
  91. acct_postop_init, POST_PLUGIN_DESC, NULL, plugin_id) != 0 ) {
  92. slapi_log_error( SLAPI_LOG_FATAL, PLUGIN_NAME,
  93. "acct_policy_init failed to register callbacks\n" );
  94. return( CALLBACK_ERR );
  95. }
  96. return( CALLBACK_OK );
  97. }
  98. /*
  99. Plugin startup function, when this is called any other plugins should
  100. already be initialized, so it's safe to e.g. perform internal searches,
  101. which is needed to retrieve the plugin configuration
  102. */
  103. int
  104. acct_policy_start( Slapi_PBlock *pb ) {
  105. acctPluginCfg *cfg;
  106. void *plugin_id = get_identity();
  107. if(slapi_plugin_running(pb)){
  108. return 0;
  109. }
  110. /* Load plugin configuration */
  111. if( acct_policy_load_config_startup( pb, plugin_id ) ) {
  112. slapi_log_error( SLAPI_LOG_FATAL, PLUGIN_NAME,
  113. "acct_policy_start failed to load configuration\n" );
  114. return( CALLBACK_ERR );
  115. }
  116. /* Show the configuration */
  117. cfg = get_config();
  118. slapi_log_error( SLAPI_LOG_PLUGIN, PLUGIN_NAME, "acct_policy_start config: "
  119. "stateAttrName=%s altStateAttrName=%s specAttrName=%s limitAttrName=%s "
  120. "alwaysRecordLogin=%d\n",
  121. cfg->state_attr_name, cfg->alt_state_attr_name?cfg->alt_state_attr_name:"not configured", cfg->spec_attr_name,
  122. cfg->limit_attr_name, cfg->always_record_login);
  123. return( CALLBACK_OK );
  124. }
  125. int
  126. acct_policy_close( Slapi_PBlock *pb )
  127. {
  128. int rc = 0;
  129. free_config();
  130. return rc;
  131. }
  132. int
  133. acct_preop_init( Slapi_PBlock *pb ) {
  134. /* Which slapi plugin API we're compatible with. */
  135. if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  136. SLAPI_PLUGIN_VERSION_01 ) != 0 ||
  137. slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  138. (void *)&pre_plugin_desc ) != 0 ) {
  139. slapi_log_error( SLAPI_LOG_FATAL, PRE_PLUGIN_NAME,
  140. "Failed to set plugin version or description\n" );
  141. return( CALLBACK_ERR );
  142. }
  143. if ( slapi_pblock_set( pb, SLAPI_PLUGIN_PRE_BIND_FN,
  144. (void *) acct_bind_preop ) != 0 ) {
  145. slapi_log_error( SLAPI_LOG_FATAL, PRE_PLUGIN_NAME,
  146. "Failed to set plugin callback function\n" );
  147. return( CALLBACK_ERR );
  148. }
  149. return( CALLBACK_OK );
  150. }
  151. int
  152. acct_postop_init( Slapi_PBlock *pb )
  153. {
  154. void *plugin_id = get_identity();
  155. if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  156. SLAPI_PLUGIN_VERSION_01 ) != 0 ||
  157. slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  158. (void *)&post_plugin_desc ) != 0 ) {
  159. slapi_log_error( SLAPI_LOG_FATAL, POST_PLUGIN_NAME,
  160. "Failed to set plugin version or name\n" );
  161. return( CALLBACK_ERR );
  162. }
  163. if ( slapi_pblock_set( pb, SLAPI_PLUGIN_POST_BIND_FN,
  164. (void *)acct_bind_postop ) != 0 ) {
  165. slapi_log_error( SLAPI_LOG_FATAL, POST_PLUGIN_NAME,
  166. "Failed to set plugin callback function\n" );
  167. return( CALLBACK_ERR );
  168. }
  169. if( slapi_pblock_get( pb, SLAPI_PLUGIN_IDENTITY, &plugin_id ) != 0 ) {
  170. slapi_log_error( SLAPI_LOG_FATAL, POST_PLUGIN_NAME,
  171. "Failed to get plugin identity\n" );
  172. return( CALLBACK_ERR );
  173. }
  174. return( CALLBACK_OK );
  175. }