acct_config.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. newcfg = get_config();
  47. rc = acct_policy_entry2config( config_entry, newcfg );
  48. slapi_entry_free( config_entry );
  49. return( rc );
  50. }
  51. /*
  52. Parses config entry into config structure, caller is responsible for
  53. allocating the config structure memory
  54. */
  55. static int
  56. acct_policy_entry2config( Slapi_Entry *e, acctPluginCfg *newcfg ) {
  57. char *config_val;
  58. int rc = 0;
  59. if( newcfg == NULL ) {
  60. slapi_log_error( SLAPI_LOG_FATAL, PLUGIN_NAME,
  61. "Failed to allocate configuration structure\n" );
  62. return( -1 );
  63. }
  64. memset( newcfg, 0, sizeof( acctPluginCfg ) );
  65. newcfg->state_attr_name = get_attr_string_val( e, CFG_LASTLOGIN_STATE_ATTR );
  66. if( newcfg->state_attr_name == NULL ) {
  67. newcfg->state_attr_name = slapi_ch_strdup( DEFAULT_LASTLOGIN_STATE_ATTR );
  68. } else if (!update_is_allowed_attr(newcfg->state_attr_name)) {
  69. /* log a warning that this attribute cannot be updated */
  70. slapi_log_error( SLAPI_LOG_FATAL, PLUGIN_NAME,
  71. "The configured state attribute [%s] cannot be updated, accounts will always become inactive.\n",
  72. newcfg->state_attr_name );
  73. }
  74. newcfg->alt_state_attr_name = get_attr_string_val( e, CFG_ALT_LASTLOGIN_STATE_ATTR );
  75. /* alt_state_attr_name should be optional, but for backward compatibility,
  76. * if not specified use a default. If the attribute is "1.1", no fallback
  77. * will be used
  78. */
  79. if( newcfg->alt_state_attr_name == NULL ) {
  80. newcfg->alt_state_attr_name = slapi_ch_strdup( DEFAULT_ALT_LASTLOGIN_STATE_ATTR );
  81. } else if ( !strcmp( newcfg->alt_state_attr_name, "1.1" ) ) {
  82. slapi_ch_free_string( &newcfg->alt_state_attr_name ); /*none - NULL */
  83. } /* else use configured value */
  84. newcfg->spec_attr_name = get_attr_string_val( e, CFG_SPEC_ATTR );
  85. if( newcfg->spec_attr_name == NULL ) {
  86. newcfg->spec_attr_name = slapi_ch_strdup( DEFAULT_SPEC_ATTR );
  87. }
  88. newcfg->limit_attr_name = get_attr_string_val( e, CFG_INACT_LIMIT_ATTR );
  89. if( newcfg->limit_attr_name == NULL ) {
  90. newcfg->limit_attr_name = slapi_ch_strdup( DEFAULT_INACT_LIMIT_ATTR );
  91. }
  92. config_val = get_attr_string_val( e, CFG_RECORD_LOGIN );
  93. if( config_val &&
  94. ( strcasecmp( config_val, "true" ) == 0 ||
  95. strcasecmp( config_val, "yes" ) == 0 ||
  96. strcasecmp( config_val, "on" ) == 0 ||
  97. strcasecmp( config_val, "1" ) == 0 ) ) {
  98. newcfg->always_record_login = 1;
  99. } else {
  100. newcfg->always_record_login = 0;
  101. }
  102. slapi_ch_free_string(&config_val);
  103. /* the default limit if not set in the acctPolicySubentry */
  104. config_val = get_attr_string_val( e, newcfg->limit_attr_name );
  105. if( config_val ) {
  106. char *endptr = NULL;
  107. newcfg->inactivitylimit = strtoul(config_val, &endptr, 10);
  108. if (endptr && (*endptr != '\0')) {
  109. slapi_log_error( SLAPI_LOG_FATAL, PLUGIN_NAME,
  110. "Failed to parse [%s] from the config entry: [%s] is not a valid unsigned long value\n",
  111. newcfg->limit_attr_name, config_val );
  112. rc = -1;
  113. newcfg->inactivitylimit = ULONG_MAX;
  114. }
  115. } else {
  116. newcfg->inactivitylimit = ULONG_MAX;
  117. }
  118. slapi_ch_free_string(&config_val);
  119. return( rc );
  120. }
  121. /*
  122. Returns a pointer to config structure for use by any code needing to look
  123. at, for example, attribute mappings
  124. */
  125. acctPluginCfg*
  126. get_config() {
  127. return( &globalcfg );
  128. }