1
0

acct_init.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. static void *_PluginID = NULL;
  57. static Slapi_DN *_PluginDN = NULL;
  58. static Slapi_DN *_ConfigAreaDN = NULL;
  59. static Slapi_RWLock *config_rwlock = NULL;
  60. void
  61. acct_policy_set_plugin_id(void *pluginID)
  62. {
  63. _PluginID = pluginID;
  64. }
  65. void *
  66. acct_policy_get_plugin_id(void)
  67. {
  68. return _PluginID;
  69. }
  70. void
  71. acct_policy_set_plugin_sdn(Slapi_DN *pluginDN)
  72. {
  73. _PluginDN = pluginDN;
  74. }
  75. Slapi_DN *
  76. acct_policy_get_plugin_sdn()
  77. {
  78. return _PluginDN;
  79. }
  80. void
  81. acct_policy_set_config_area(Slapi_DN *sdn)
  82. {
  83. _ConfigAreaDN = sdn;
  84. }
  85. Slapi_DN *
  86. acct_policy_get_config_area()
  87. {
  88. return _ConfigAreaDN;
  89. }
  90. /*
  91. Master init function for the account plugin
  92. */
  93. int
  94. acct_policy_init( Slapi_PBlock *pb )
  95. {
  96. void *plugin_id;
  97. int enabled;
  98. slapi_pblock_get(pb, SLAPI_PLUGIN_ENABLED, &enabled);
  99. if (!enabled) {
  100. /* not enabled */
  101. return( CALLBACK_OK );
  102. }
  103. if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  104. SLAPI_PLUGIN_VERSION_01 ) != 0 ||
  105. slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  106. (void *)&plugin_desc ) != 0 ||
  107. slapi_pblock_set( pb, SLAPI_PLUGIN_CLOSE_FN,
  108. (void *)&acct_policy_close ) != 0 ||
  109. slapi_pblock_set( pb, SLAPI_PLUGIN_START_FN,
  110. (void *)acct_policy_start ) != 0 ) {
  111. slapi_log_error(SLAPI_LOG_ERR, PLUGIN_NAME,
  112. "acct_policy_init - Registration failed\n" );
  113. return( CALLBACK_ERR );
  114. }
  115. if( slapi_pblock_get( pb, SLAPI_PLUGIN_IDENTITY, &plugin_id ) != 0 ) {
  116. slapi_log_error(SLAPI_LOG_ERR, PLUGIN_NAME,
  117. "acct_policy_init - Failed to get plugin identity\n" );
  118. return( CALLBACK_ERR );
  119. }
  120. set_identity( plugin_id );
  121. /* Register the pre and postop plugins */
  122. if( slapi_register_plugin("preoperation", 1, "acct_preop_init",
  123. acct_preop_init, PRE_PLUGIN_DESC, NULL, plugin_id) != 0 ||
  124. slapi_register_plugin("postoperation", 1, "acct_postop_init",
  125. acct_postop_init, POST_PLUGIN_DESC, NULL, plugin_id) != 0 ) {
  126. slapi_log_error(SLAPI_LOG_ERR, PLUGIN_NAME,
  127. "acct_policy_init - Failed to register callbacks\n" );
  128. return( CALLBACK_ERR );
  129. }
  130. return( CALLBACK_OK );
  131. }
  132. /*
  133. Plugin startup function, when this is called any other plugins should
  134. already be initialized, so it's safe to e.g. perform internal searches,
  135. which is needed to retrieve the plugin configuration
  136. */
  137. int
  138. acct_policy_start( Slapi_PBlock *pb )
  139. {
  140. acctPluginCfg *cfg;
  141. void *plugin_id = get_identity();
  142. Slapi_DN *plugindn = NULL;
  143. char *config_area = NULL;
  144. if(slapi_plugin_running(pb)){
  145. return 0;
  146. }
  147. slapi_pblock_get(pb, SLAPI_TARGET_SDN, &plugindn);
  148. acct_policy_set_plugin_sdn(slapi_sdn_dup(plugindn));
  149. /* Set the alternate config area if one is defined. */
  150. slapi_pblock_get(pb, SLAPI_PLUGIN_CONFIG_AREA, &config_area);
  151. if (config_area) {
  152. acct_policy_set_config_area(slapi_sdn_new_normdn_byval(config_area));
  153. }
  154. if(config_rwlock == NULL){
  155. if((config_rwlock = slapi_new_rwlock()) == NULL){
  156. return( CALLBACK_ERR );
  157. }
  158. }
  159. /* Load plugin configuration */
  160. if( acct_policy_load_config_startup( pb, plugin_id ) ) {
  161. slapi_log_error(SLAPI_LOG_ERR, PLUGIN_NAME,
  162. "acct_policy_start failed to load configuration\n" );
  163. return( CALLBACK_ERR );
  164. }
  165. /* Show the configuration */
  166. cfg = get_config();
  167. slapi_log_error(SLAPI_LOG_PLUGIN, PLUGIN_NAME, "acct_policy_start - config: "
  168. "stateAttrName=%s altStateAttrName=%s specAttrName=%s limitAttrName=%s "
  169. "alwaysRecordLogin=%d\n",
  170. cfg->state_attr_name, cfg->alt_state_attr_name?cfg->alt_state_attr_name:"not configured", cfg->spec_attr_name,
  171. cfg->limit_attr_name, cfg->always_record_login);
  172. return( CALLBACK_OK );
  173. }
  174. int
  175. acct_policy_close( Slapi_PBlock *pb )
  176. {
  177. int rc = 0;
  178. slapi_destroy_rwlock(config_rwlock);
  179. config_rwlock = NULL;
  180. slapi_sdn_free(&_PluginDN);
  181. slapi_sdn_free(&_ConfigAreaDN);
  182. free_config();
  183. return rc;
  184. }
  185. int
  186. acct_preop_init( Slapi_PBlock *pb ) {
  187. /* Which slapi plugin API we're compatible with. */
  188. if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  189. SLAPI_PLUGIN_VERSION_01 ) != 0 ||
  190. slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  191. (void *)&pre_plugin_desc ) != 0 ) {
  192. slapi_log_error(SLAPI_LOG_ERR, PRE_PLUGIN_NAME,
  193. "Failed to set plugin version or description\n" );
  194. return( CALLBACK_ERR );
  195. }
  196. if ( slapi_pblock_set( pb, SLAPI_PLUGIN_PRE_BIND_FN, (void *) acct_bind_preop ) != 0 ||
  197. slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_ADD_FN, (void *) acct_add_pre_op) != 0 ||
  198. slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_MODIFY_FN, (void *) acct_mod_pre_op) != 0 ||
  199. slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_DELETE_FN, (void *) acct_del_pre_op) != 0)
  200. {
  201. slapi_log_error(SLAPI_LOG_ERR, PRE_PLUGIN_NAME,
  202. "acct_preop_init - Failed to set plugin callback function\n" );
  203. return( CALLBACK_ERR );
  204. }
  205. return( CALLBACK_OK );
  206. }
  207. int
  208. acct_postop_init( Slapi_PBlock *pb )
  209. {
  210. void *plugin_id = get_identity();
  211. if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  212. SLAPI_PLUGIN_VERSION_01 ) != 0 ||
  213. slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  214. (void *)&post_plugin_desc ) != 0 ) {
  215. slapi_log_error(SLAPI_LOG_ERR, POST_PLUGIN_NAME,
  216. "acct_postop_init - Failed to set plugin version or name\n" );
  217. return( CALLBACK_ERR );
  218. }
  219. if ( slapi_pblock_set( pb, SLAPI_PLUGIN_POST_BIND_FN, (void *)acct_bind_postop ) != 0 ||
  220. slapi_pblock_set(pb, SLAPI_PLUGIN_POST_ADD_FN, (void *) acct_post_op) != 0 ||
  221. slapi_pblock_set(pb, SLAPI_PLUGIN_POST_MODIFY_FN, (void *) acct_post_op) != 0)
  222. {
  223. slapi_log_error(SLAPI_LOG_ERR, POST_PLUGIN_NAME,
  224. "acct_postop_init - Failed to set plugin callback function\n" );
  225. return( CALLBACK_ERR );
  226. }
  227. if( slapi_pblock_get( pb, SLAPI_PLUGIN_IDENTITY, &plugin_id ) != 0 ) {
  228. slapi_log_error(SLAPI_LOG_ERR, POST_PLUGIN_NAME,
  229. "acct_postop_init - Failed to get plugin identity\n" );
  230. return( CALLBACK_ERR );
  231. }
  232. return( CALLBACK_OK );
  233. }
  234. /*
  235. * Wrappers for config locking
  236. */
  237. void
  238. config_rd_lock()
  239. {
  240. slapi_rwlock_rdlock(config_rwlock);
  241. }
  242. void
  243. config_wr_lock()
  244. {
  245. slapi_rwlock_wrlock(config_rwlock);
  246. }
  247. void
  248. config_unlock()
  249. {
  250. slapi_rwlock_unlock(config_rwlock);
  251. }