acct_config.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #include <stdio.h>
  17. #include <string.h>
  18. #include "slapi-plugin.h"
  19. #include "acctpolicy.h"
  20. #include "nspr.h"
  21. /* Globals */
  22. static acctPluginCfg globalcfg;
  23. /* Local function prototypes */
  24. static int acct_policy_entry2config( Slapi_Entry *e,
  25. acctPluginCfg *newcfg );
  26. /*
  27. Creates global config structure from config entry at plugin startup
  28. */
  29. int
  30. acct_policy_load_config_startup( Slapi_PBlock* pb, void* plugin_id ) {
  31. acctPluginCfg *newcfg;
  32. Slapi_Entry *config_entry = NULL;
  33. Slapi_DN *config_sdn = NULL;
  34. int rc;
  35. /* Retrieve the config entry */
  36. config_sdn = slapi_sdn_new_normdn_byref( PLUGIN_CONFIG_DN );
  37. rc = slapi_search_internal_get_entry( config_sdn, NULL, &config_entry,
  38. plugin_id);
  39. slapi_sdn_free( &config_sdn );
  40. if( rc != LDAP_SUCCESS || config_entry == NULL ) {
  41. slapi_log_error( SLAPI_LOG_FATAL, PLUGIN_NAME,
  42. "Failed to retrieve configuration entry %s: %d\n",
  43. PLUGIN_CONFIG_DN, rc );
  44. return( -1 );
  45. }
  46. config_wr_lock();
  47. free_config();
  48. newcfg = get_config();
  49. rc = acct_policy_entry2config( config_entry, newcfg );
  50. config_unlock();
  51. slapi_entry_free( config_entry );
  52. return( rc );
  53. }
  54. /*
  55. Parses config entry into config structure, caller is responsible for
  56. allocating the config structure memory
  57. */
  58. static int
  59. acct_policy_entry2config( Slapi_Entry *e, acctPluginCfg *newcfg ) {
  60. char *config_val;
  61. int rc = 0;
  62. if( newcfg == NULL ) {
  63. slapi_log_error( SLAPI_LOG_FATAL, PLUGIN_NAME,
  64. "Failed to allocate configuration structure\n" );
  65. return( -1 );
  66. }
  67. memset( newcfg, 0, sizeof( acctPluginCfg ) );
  68. newcfg->state_attr_name = get_attr_string_val( e, CFG_LASTLOGIN_STATE_ATTR );
  69. if( newcfg->state_attr_name == NULL ) {
  70. newcfg->state_attr_name = slapi_ch_strdup( DEFAULT_LASTLOGIN_STATE_ATTR );
  71. } else if (!update_is_allowed_attr(newcfg->state_attr_name)) {
  72. /* log a warning that this attribute cannot be updated */
  73. slapi_log_error( SLAPI_LOG_FATAL, PLUGIN_NAME,
  74. "The configured state attribute [%s] cannot be updated, accounts will always become inactive.\n",
  75. newcfg->state_attr_name );
  76. }
  77. newcfg->alt_state_attr_name = get_attr_string_val( e, CFG_ALT_LASTLOGIN_STATE_ATTR );
  78. /* alt_state_attr_name should be optional, but for backward compatibility,
  79. * if not specified use a default. If the attribute is "1.1", no fallback
  80. * will be used
  81. */
  82. if( newcfg->alt_state_attr_name == NULL ) {
  83. newcfg->alt_state_attr_name = slapi_ch_strdup( DEFAULT_ALT_LASTLOGIN_STATE_ATTR );
  84. } else if ( !strcmp( newcfg->alt_state_attr_name, "1.1" ) ) {
  85. slapi_ch_free_string( &newcfg->alt_state_attr_name ); /*none - NULL */
  86. } /* else use configured value */
  87. newcfg->always_record_login_attr = get_attr_string_val( e, CFG_RECORD_LOGIN_ATTR );
  88. /* What user attribute will store the last login time
  89. * of a user. If empty, should have the same value as
  90. * stateattrname. default value: empty
  91. */
  92. if( newcfg->always_record_login_attr == NULL ) {
  93. newcfg->always_record_login_attr = slapi_ch_strdup( newcfg->state_attr_name );
  94. }
  95. newcfg->spec_attr_name = get_attr_string_val( e, CFG_SPEC_ATTR );
  96. if( newcfg->spec_attr_name == NULL ) {
  97. newcfg->spec_attr_name = slapi_ch_strdup( DEFAULT_SPEC_ATTR );
  98. }
  99. newcfg->limit_attr_name = get_attr_string_val( e, CFG_INACT_LIMIT_ATTR );
  100. if( newcfg->limit_attr_name == NULL ) {
  101. newcfg->limit_attr_name = slapi_ch_strdup( DEFAULT_INACT_LIMIT_ATTR );
  102. }
  103. config_val = get_attr_string_val( e, CFG_RECORD_LOGIN );
  104. if( config_val &&
  105. ( strcasecmp( config_val, "true" ) == 0 ||
  106. strcasecmp( config_val, "yes" ) == 0 ||
  107. strcasecmp( config_val, "on" ) == 0 ||
  108. strcasecmp( config_val, "1" ) == 0 ) ) {
  109. newcfg->always_record_login = 1;
  110. } else {
  111. newcfg->always_record_login = 0;
  112. }
  113. slapi_ch_free_string(&config_val);
  114. /* the default limit if not set in the acctPolicySubentry */
  115. config_val = get_attr_string_val( e, newcfg->limit_attr_name );
  116. if( config_val ) {
  117. char *endptr = NULL;
  118. newcfg->inactivitylimit = strtoul(config_val, &endptr, 10);
  119. if (endptr && (*endptr != '\0')) {
  120. slapi_log_error( SLAPI_LOG_FATAL, PLUGIN_NAME,
  121. "Failed to parse [%s] from the config entry: [%s] is not a valid unsigned long value\n",
  122. newcfg->limit_attr_name, config_val );
  123. rc = -1;
  124. newcfg->inactivitylimit = ULONG_MAX;
  125. }
  126. } else {
  127. newcfg->inactivitylimit = ULONG_MAX;
  128. }
  129. slapi_ch_free_string(&config_val);
  130. return( rc );
  131. }
  132. /*
  133. Returns a pointer to config structure for use by any code needing to look
  134. at, for example, attribute mappings
  135. */
  136. acctPluginCfg*
  137. get_config() {
  138. return( &globalcfg );
  139. }
  140. void
  141. free_config()
  142. {
  143. slapi_ch_free_string(&globalcfg.state_attr_name);
  144. slapi_ch_free_string(&globalcfg.alt_state_attr_name);
  145. slapi_ch_free_string(&globalcfg.spec_attr_name);
  146. slapi_ch_free_string(&globalcfg.limit_attr_name);
  147. slapi_ch_free_string(&globalcfg.always_record_login_attr);
  148. }