summaryrefslogtreecommitdiff
path: root/plugin/plugin.c
blob: 329f6b55897e2178208460f987828d29505d108e (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
 * Copyright (c) 2009, Stefan Walter
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions 
 * are met:
 * 
 *     * Redistributions of source code must retain the above 
 *       copyright notice, this list of conditions and the 
 *       following disclaimer.
 *     * Redistributions in binary form must reproduce the 
 *       above copyright notice, this list of conditions and 
 *       the following disclaimer in the documentation and/or 
 *       other materials provided with the distribution.
 *     * The names of contributors to this software may not be 
 *       used to endorse or promote products derived from this 
 *       software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
 * DAMAGE.
 */

#include "config.h"

#include "plugin.h"

#include <sys/wait.h>

#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <ctype.h>
#ifdef WITH_SYSLOG
#include <syslog.h>
#endif

#define WHITESPACE " \t\r\n\v"

static char **plugin_arguments = NULL;

/* ---------------------------------------------------------------------
 * LOGGING/TRACING
 */

static void 
log_msg_va (int level, const char* msg, va_list ap)
{
	size_t len;
	char buf[1024];

	assert (level >= 0);
	assert (msg != NULL);

	vsnprintf (buf, sizeof (buf) - 2, msg, ap);

	len = strlen (buf);
	buf[len] = '\n';
	buf[len + 1] = '\0';  

	fprintf (stderr, "%s: %s", PLUGIN_NAME, buf);
#ifdef WITH_SYSLOG
	if (level != SLAPI_LOG_TRACE)
		syslog (LOG_WARNING | LOG_DAEMON, "%s: %s", PLUGIN_NAME, buf);
#endif
	slapi_log_error (level, PLUGIN_NAME, buf);
}

void 
log_msg (int level, const char* msg, ...)
{
	va_list ap;
	va_start (ap, msg);
	log_msg_va (level, msg, ap);
	va_end (ap);
}

#if _DEBUG
void 
log_trace (const char *where, int line, const char *msg)
{
	log_msg (SLAPI_LOG_TRACE, " *** %s:%d %s%s", 
	         where, line, msg ? ": " : "", msg ? msg : "");
}
#endif

void
log_plugin (const char* msg, ...)
{
	va_list ap;
	va_start (ap, msg);
	log_msg_va (SLAPI_LOG_PLUGIN, msg, ap);
	va_end (ap);
}

/* --------------------------------------------------------------------------------
 * HELPERS
 */

const char* 
ltrim (const char *data)
{
	while (*data && isspace (*data))
		++data;
	return data;
}

void
rtrim (char *data)
{
	char *t = data + strlen (data);
	while (t > data && isspace (*(t - 1))) {
		t--;
		*t = 0;
	}
}

char* 
trim (char *data)
{
	data = (char*)ltrim (data);
	rtrim (data);
	return data;
}

void
lowercase (char *data)
{
	while (*data) {
		*data = tolower (*data);
		++data;
	}
}

/* ---------------------------------------------------------------------------------
 * OPERATIONS
 */

static int
pre_modify (Slapi_PBlock *pb)
{
	char *dn;
	int rc;
	
	return_val_if_fail (pb, -1);
	
	/* Get out the DN and normalize it */
	rc = slapi_pblock_get (pb, SLAPI_MODIFY_TARGET, &dn);
	return_val_if_fail (rc >= 0 && dn, -1);
	dn = slapi_ch_strdup (dn);
	slapi_dn_normalize_case (dn);

	/* Send off to components */
	rc = suffix_pre_modify (pb, dn);
	
	slapi_ch_free_string (&dn);
	return rc;
}

static int 
pre_add (Slapi_PBlock *pb)
{
	char *dn;
	int rc;
	
	return_val_if_fail (pb, -1);
		
	/* Get out the DN and normalize it */
	rc = slapi_pblock_get (pb, SLAPI_ADD_TARGET, &dn);
	return_val_if_fail (rc >= 0 && dn, -1);
	dn = slapi_ch_strdup (dn);
	slapi_dn_normalize_case (dn);
	
	/* Send off to components */
	rc = suffix_pre_add (pb, dn);
	
	slapi_ch_free_string (&dn);
	return rc;
}

static Slapi_PluginDesc plugin_description = {
	PLUGIN_NAME,                                              /* plug-in identifier */
	"stef@memberwebs.com",                                    /* vendor name */
	VERSION,                                                  /* plug-in revision number */
	"Limit attr values to those that have a suffix which equals parent's attribute"
};

static int
plugin_destroy (Slapi_PBlock *pb) 
{
	suffix_destroy ();
	
	slapi_ch_array_free (plugin_arguments);
	plugin_arguments = NULL;
	
	return 0;
}

int 
plugin_init (Slapi_PBlock *pb)
{
	char **argv = NULL, *arg, *value;
	int argc = 0;
	int rc, i;
	
	return_val_if_fail (pb, -1);

	rc = slapi_pblock_get (pb, SLAPI_PLUGIN_ARGV, &argv);
	return_val_if_fail (rc >= 0, -1);
	slapi_pblock_get (pb, SLAPI_PLUGIN_ARGC, &argc);
	return_val_if_fail (rc >= 0, -1);
	
	/* 
	 * Copy all the arguments, until we get destroyed, and 
	 * send the arguments to the components to configure 
	 * themselves.
	 */
	plugin_arguments = (char**)slapi_ch_calloc (argc + 1, sizeof (char*));
	for (i = 0; i < argc; ++i) {
		plugin_arguments[i] = slapi_ch_strdup (argv[i]);
		arg = trim (plugin_arguments[i]);
		
		value = arg + strcspn (arg, ":=");
		if (!*value) {
			value = NULL;
		} else {
			*value = 0;
			value = trim (value + 1);
		}
		
		lowercase (arg);
		
		rc = suffix_config (arg, value);
		
		if (!rc)
			log_plugin ("unrecognized plugin argument: %s", argv[i]);
	}
	
	/* Null terminate */
	plugin_arguments[i] = NULL;

	/* Next we initialize all components */
	if (suffix_init () < 0)
		return -1;
	
	if (slapi_pblock_set (pb, SLAPI_PLUGIN_VERSION, SLAPI_PLUGIN_VERSION_03) != 0 ||
	    slapi_pblock_set (pb, SLAPI_PLUGIN_DESCRIPTION, &plugin_description) != 0 ||
	    slapi_pblock_set (pb, SLAPI_PLUGIN_DESTROY_FN, plugin_destroy)) {
		log_plugin ("error registring plugin");
		return -1;
	}

	/* Setup the entry add/mobify functions */
	if (slapi_pblock_set (pb, SLAPI_PLUGIN_PRE_ADD_FN, (void*)pre_add) != 0 ||
	    slapi_pblock_set (pb, SLAPI_PLUGIN_PRE_MODIFY_FN, (void*)pre_modify) != 0) {
		log_plugin ("error registering plugin hooks");
		return -1;
	}
	
	trace (PLUGIN_NAME " initialized");
	return 0;
}