summaryrefslogtreecommitdiffstats
path: root/ldap/servers/plugins/acctpolicy/acct_init.c
blob: 6f33434c264de1bc1e32dff027101080cdda277e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/******************************************************************************
Copyright (C) 2009 Hewlett-Packard Development Company, L.P.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

Contributors:
Hewlett-Packard Development Company, L.P.
******************************************************************************/

/* Example enabling and config entries
dn: cn=Account Policy Plugin,cn=plugins,cn=config
objectClass: top
objectClass: nsSlapdPlugin
objectClass: extensibleObject
cn: Account Policy Plugin
nsslapd-pluginPath: /path/to/libacctpolicy-plugin.sl
nsslapd-pluginInitfunc: acct_policy_init
nsslapd-pluginType: object
nsslapd-pluginEnabled: on
nsslapd-plugin-depends-on-type: database
nsslapd-pluginId: Account Policy Plugin

dn: cn=config,cn=Account Policy Plugin,cn=plugins,cn=config
objectClass: top
objectClass: extensibleObject
cn: config
alwaysrecordlogin: yes
stateattrname: lastLoginTime
altstateattrname: createTimestamp
specattrname: acctPolicySubentry
limitattrname: accountInactivityLimit
*/

#include <stdio.h>
#include <string.h>
#include "slapi-plugin.h"
#include "acctpolicy.h"

static Slapi_PluginDesc plugin_desc = { PLUGIN_NAME, PLUGIN_VENDOR,
				PLUGIN_VERSION, PLUGIN_DESC };
static Slapi_PluginDesc pre_plugin_desc = { PRE_PLUGIN_NAME, PLUGIN_VENDOR,
				PLUGIN_VERSION, PLUGIN_DESC };
static Slapi_PluginDesc post_plugin_desc = { PRE_PLUGIN_NAME, PLUGIN_VENDOR,
				PLUGIN_VERSION, PLUGIN_DESC };

/* Local function prototypes */
int acct_policy_start( Slapi_PBlock *pb );
int acct_policy_init( Slapi_PBlock *pb );
int acct_preop_init( Slapi_PBlock *pb );
int acct_postop_init( Slapi_PBlock *pb );
int acct_bind_preop( Slapi_PBlock *pb );
int acct_bind_postop( Slapi_PBlock *pb );

/*
  Master init function for the account plugin
*/
int
acct_policy_init( Slapi_PBlock *pb )
{
	void *plugin_id;

	if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
				SLAPI_PLUGIN_VERSION_01 ) != 0 ||
		slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
				(void *)&plugin_desc ) != 0 || 
		slapi_pblock_set( pb, SLAPI_PLUGIN_START_FN,
				(void *)acct_policy_start ) != 0 ) {
			slapi_log_error( SLAPI_LOG_FATAL, PLUGIN_NAME,
					"acct_policy_init registration failed\n" );
			return( CALLBACK_ERR );
	}

	if( slapi_pblock_get( pb, SLAPI_PLUGIN_IDENTITY, &plugin_id ) != 0 ) {
		slapi_log_error( SLAPI_LOG_FATAL, PLUGIN_NAME,
				"acct_policy_init failed to get plugin identity\n" );
		return( CALLBACK_ERR );
	}

	set_identity( plugin_id );

	/* Register the pre and postop plugins */
	if( slapi_register_plugin("preoperation", 1, "acct_preop_init",
		acct_preop_init, PRE_PLUGIN_DESC, NULL, plugin_id) != 0 ||
		slapi_register_plugin("postoperation", 1, "acct_postop_init",
		acct_postop_init, POST_PLUGIN_DESC, NULL, plugin_id) != 0 ) {
		slapi_log_error( SLAPI_LOG_FATAL, PLUGIN_NAME,
			"acct_policy_init failed to register callbacks\n" );
		return( CALLBACK_ERR );
	}

	return( CALLBACK_OK );
}

/*
  Plugin startup function, when this is called any other plugins should
  already be initialized, so it's safe to e.g. perform internal searches,
  which is needed to retrieve the plugin configuration
*/
int
acct_policy_start( Slapi_PBlock *pb ) {
	acctPluginCfg *cfg;
	void *plugin_id = get_identity();

	/* Load plugin configuration */
	if( acct_policy_load_config_startup( pb, plugin_id ) ) {
		slapi_log_error( SLAPI_LOG_FATAL, PLUGIN_NAME,
			"acct_policy_start failed to load configuration\n" );
		return( CALLBACK_ERR );
	}

	/* Show the configuration */
	cfg = get_config();
	slapi_log_error( SLAPI_LOG_PLUGIN, PLUGIN_NAME, "acct_policy_start config: "
		"stateAttrName=%s altStateAttrName=%s specAttrName=%s limitAttrName=%s "
		"alwaysRecordLogin=%d\n",
		cfg->state_attr_name, cfg->alt_state_attr_name, cfg->spec_attr_name,
		cfg->limit_attr_name, cfg->always_record_login);
	return( CALLBACK_OK );
}

int
acct_preop_init( Slapi_PBlock *pb ) {
	/* Which slapi plugin API we're compatible with. */
	if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
			SLAPI_PLUGIN_VERSION_01 ) != 0 ||
		slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
					(void *)&pre_plugin_desc ) != 0 ) {
		slapi_log_error( SLAPI_LOG_FATAL, PRE_PLUGIN_NAME,
					"Failed to set plugin version or description\n" );
		return( CALLBACK_ERR );
	}

	if ( slapi_pblock_set( pb, SLAPI_PLUGIN_PRE_BIND_FN,
				(void *) acct_bind_preop ) != 0 ) {
		slapi_log_error( SLAPI_LOG_FATAL, PRE_PLUGIN_NAME,
				"Failed to set plugin callback function\n" );
		return( CALLBACK_ERR );
	}

	return( CALLBACK_OK );
}

int
acct_postop_init( Slapi_PBlock *pb )
{
	void *plugin_id = get_identity();

	if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
				SLAPI_PLUGIN_VERSION_01 ) != 0 ||
		slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
					(void *)&post_plugin_desc ) != 0 ) {
			slapi_log_error( SLAPI_LOG_FATAL, POST_PLUGIN_NAME,
						"Failed to set plugin version or name\n" );
			return( CALLBACK_ERR );
	}

	if ( slapi_pblock_set( pb, SLAPI_PLUGIN_POST_BIND_FN,
				(void *)acct_bind_postop ) != 0 ) {
		slapi_log_error( SLAPI_LOG_FATAL, POST_PLUGIN_NAME,
				"Failed to set plugin callback function\n" );
		return( CALLBACK_ERR );
	}

	if( slapi_pblock_get( pb, SLAPI_PLUGIN_IDENTITY, &plugin_id ) != 0 ) {
		slapi_log_error( SLAPI_LOG_FATAL, POST_PLUGIN_NAME,
				"Failed to get plugin identity\n" );
		return( CALLBACK_ERR );
	}

	return( CALLBACK_OK );
}