summaryrefslogtreecommitdiffstats
path: root/src/format.c
blob: b6cc37d01abb85a0c590a42ebcb62dbc805f4855 (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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#include <sys/types.h>
#include <rpcsvc/yp.h>
#include <stdlib.h>
#include "defaults.h"
#include "format.h"
#include "plugin.h"

void
format_free_data(char *data)
{
	if (data != NULL) {
		free(data);
	}
}

void
format_free_ndn_list(char **ndn_list)
{
	if (ndn_list != NULL) {
		free(ndn_list);
	}
}

/* Retrieve a single value for an attribute.  If there are no values, or more
 * than one, fail. */
static char *
format_single(Slapi_PBlock *pb, Slapi_Entry *e, const char *attr,
	      char ***visited_ndns)
{
	char **values, *ret;
	int count;
	values = slapi_entry_attr_get_charray(e, attr);
	for (count = 0; (values != NULL) && (values[count] != NULL); count++) {
		continue;
	}
	if (count == 1) {
		ret = strdup(values[0]);
	} else {
		ret = NULL;
	}
	slapi_ch_array_free(values);
	return ret;
}

/* Find the matching closing marker. */
static const char *
format_find_closer(const char *pair, const char *pattern)
{
	int i, level = 0;
	for (i = 0; pattern[i] != '\0'; i++) {
		if (pattern[i] == pair[0]) {
			level++;
		} else {
			if (pattern[i] == pair[1]) {
				level--;
			}
		}
		if (level == 0) {
			return &pattern[i];
		}
	}
	return NULL;
}

/* Recursively expand the expression into the output buffer, adding any entries
 * we visit (other than e) to the list of visited NDNs.  Unless it's a literal,
 * treat the entire input format specifier as an expression. */
static int
format_expand(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
	      const char *fmt,
	      char *outbuf, int outbuf_len,
	      char ***visited_ndns,
	      PRBool literal)
{
	int i, j;
	int exp_len, level;
	const char *fmtstart, *fmtend, *match, *attribute;
	char *tmp, *fnname, *spd_id;
	char exp[outbuf_len * 2];
	const char *default_value, *alternate_value, *paramstart, *paramend;
	size_t spn;

	spd_id = state->plugin_desc->spd_id;
	slapi_log_error(SLAPI_LOG_PLUGIN, spd_id,
			"expanding %s\"%s\"%s\n",
			literal ? "" : "%{", fmt, literal ? "" : "}");

	/* First, expand any subexpressions and call any "functions". */
	level = 0;
	i = 0;
	j = 0;
	while (fmt[i] != '\0') {
		switch (fmt[i]) {
		case '%':
			/* This might be a subexpression, a "function" call, or
			 * an escaped character. */
			switch (fmt[i + 1]) {
			case '%':
				/* It's just an escaped "%". */
				exp[j++] = '%';
				i += 2;
				continue;
				break;
			case '{':
				/* Find the beginning of the subexpression. */
				fmtstart = fmt + i;
				/* Find the end of the subexpression. */
				match = format_find_closer("{}", fmtstart + 1);
				if (match == NULL) {
					slapi_log_error(SLAPI_LOG_PLUGIN,
							spd_id,
							"expansion failed: "
							"no closing brace\n");
					return -1;
				} else {
					/* Mark the first character after the
					 * subexpression. */
					fmtend = match + 1;
					/* Make a copy of the subexpression. */
					tmp = malloc(fmtend - fmtstart);
					if (tmp == NULL) {
						slapi_log_error(SLAPI_LOG_PLUGIN,
								spd_id,
								"expansion "
								"failed: out "
								"of memory\n");
						return -1;
					}
					memcpy(tmp, fmtstart + 2,
					       fmtend - fmtstart - 3);
					tmp[fmtend - fmtstart - 3] = '\0';
					/* Recursively expand the
					 * subexpression. */
					exp_len = format_expand(state, pb, e,
								tmp,
								exp + j,
								sizeof(exp) - j,
								visited_ndns,
								FALSE);
					free(tmp);
					if ((exp_len < 0) ||
					    (exp_len + j >= (int) sizeof(exp))) {
						/* We'd be out of space,
						 * FAIL. */
						slapi_log_error(SLAPI_LOG_PLUGIN,
								spd_id,
								"expansion "
								"failed: result"
								" would be too "
								"big\n");
						return -1;
					} else {
						/* It fit, so keep going. */
						i = (match + 1) - fmt;
						j += exp_len;
					}
				}
				continue;
				break;
			default:
				/* Assume it's a "function" call.  Pick out the
				 * name of the function. */
				paramstart = strchr(fmt + i, '{');
				if (paramstart == NULL) {
					/* No start? Bad format. */
					slapi_log_error(SLAPI_LOG_PLUGIN,
							spd_id,
							"expansion failed: "
							"bad function "
							"invocation\n");
					return -1;
				}
				paramend = strchr(paramstart + 1, '}');
				if (paramend == NULL) {
					/* No matching end? Bad format. */
					slapi_log_error(SLAPI_LOG_PLUGIN,
							spd_id,
							"expansion failed: "
							"bad function "
							"invocation\n");
					return -1;
				}
				fnname = malloc(paramstart - (fmt + i));
				if (fnname == NULL) {
					/* Out of memory, FAIL. */
					slapi_log_error(SLAPI_LOG_PLUGIN,
							spd_id,
							"expansion failed: "
							"out of memory\n");
					return -1;
				}
				memcpy(fnname, fmt + i + 1,
				       paramstart - (fmt + i + 1));
				fnname[paramstart - (fmt + i + 1)] = '\0';
				/* Isolate the parameter string. */
				tmp = malloc(paramend - paramstart);
				if (tmp == NULL) {
					/* Out of memory, FAIL. */
					slapi_log_error(SLAPI_LOG_PLUGIN,
							spd_id,
							"expansion failed: "
							"out of memory\n");
					free(fnname);
					return -1;
				}
				memcpy(tmp, paramstart + 1,
				       paramend - (paramstart + 1));
				tmp[paramend - paramstart] = '\0';
				slapi_log_error(SLAPI_LOG_PLUGIN, spd_id,
						"calling \"%s\"(\"%s\")\n",
						fnname, tmp);
				free(fnname);
				free(tmp);
				i = (paramend - fmt) + 1;
				continue;
				break;
			}
			break;
		default:
			/* Default is just a literal character. */
			exp[j++] = fmt[i++];
			break;
		}
	}
	exp[j] = '\0';

	if (literal) {
		/* It's a literal string, so we're actually done. */
		i = strlen(exp);
		if (i <= outbuf_len) {
			memcpy(outbuf, exp, i);
		}
		slapi_log_error(SLAPI_LOG_PLUGIN, "nis-plugin",
				"expanded to \"%s\"\n", exp);
		return i;
	} else {
		/* It's an expression, so evaluate it. */
		slapi_log_error(SLAPI_LOG_PLUGIN, "nis-plugin",
				"looking up simple expression \%\{\"%s\"}\n", exp);
		/* Check if it uses a default/alternate value. */
		spn = strcspn(exp, ":");
		if (spn == strlen(exp)) {
			/* Simple expression: expect it to be a single-valued
			 * attribute. */
			tmp = format_single(pb, e, exp, visited_ndns);
			if (tmp != NULL) {
				/* Copy the string to the output buffer if
				 * there's space for it. */
				i = strlen(tmp);
				if (i <= outbuf_len) {
					memcpy(outbuf, tmp, i);
				}
				slapi_log_error(SLAPI_LOG_PLUGIN, "nis-plugin",
						"expanded to \"%s\"\n", tmp);
				free(tmp);
				/* Return the length of the expanded
				 * expression. */
				return i;
			} else {
				/* No value found? FAIL. */
				slapi_log_error(SLAPI_LOG_PLUGIN, "nis-plugin",
						"no value for \"%s\"\n", exp);
				return -1;
			}
		} else {
			/* Make a copy of the attribute name. */
			exp[spn] = '\0';
			attribute = exp;
			alternate_value = NULL;
			default_value = NULL;
			/* Figure out if there's an alternate or default value
			 * given. */
			switch (exp[spn + 1]) {
			case '+':
				alternate_value = exp + spn + 2;
				break;
			case '-':
				default_value = exp + spn + 2;
				break;
			default:
				default_value = exp + spn + 1;
				break;
			}
			/* Retrieve the value. */
			tmp = format_single(pb, e, attribute, visited_ndns);
			if (tmp == NULL) {
				/* The attribute is undefined, or we're
				 * treating it as if it is. */
				if (default_value != NULL) {
					/* Supply the default value. */
					i = strlen(default_value);
					if (i <= outbuf_len) {
						memcpy(outbuf, default_value,
						       i);
					}
					slapi_log_error(SLAPI_LOG_PLUGIN, "nis-plugin",
							"expanded to \"%s\"\n", default_value);
					return i;
				} else {
					/* No value, and no default: FAIL. */
		slapi_log_error(SLAPI_LOG_PLUGIN, "nis-plugin",
				"failed to expand: no valid value, no default\n");
					return -1;
				}
			} else {
				/* There's no value defined (or it's
				 * multi-valued, which is usually trouble). */
				if (alternate_value != NULL) {
					/* Supply the alternate value. */
					i = strlen(alternate_value);
					if (i <= outbuf_len) {
						memcpy(outbuf, alternate_value,
						       i);
					}
					slapi_log_error(SLAPI_LOG_PLUGIN, "nis-plugin",
							"expanded to \"%s\"\n", alternate_value);
					free(tmp);
					return i;
				} else {
					/* Supply the looked-up value. */
					i = strlen(tmp);
					if (i <= outbuf_len) {
						memcpy(outbuf, tmp, i);
					}
					slapi_log_error(SLAPI_LOG_PLUGIN, "nis-plugin",
							"expanded to \"%s\"\n", tmp);
					free(tmp);
					return i;
				}
			}
		}
	}
}

static char *
format_format(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
	      const char *fmt, char ***visited_ndns)
{
	char buf[YPMAXRECORD], *tmp;
	const char *match, *fmtstart, *fmtend;
	unsigned int i, j;
	int exp_len;

	i = format_expand(state, pb, e, fmt, buf, sizeof(buf),
			  visited_ndns, TRUE);
	if (i < sizeof(buf)) {
		buf[i] = '\0';
		return strdup(buf);
	} else {
		return NULL;
	}
}

char *
format_get_data(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
		const char *fmt, char ***visited_ndns)
{
	Slapi_PBlock *local_pb;
	char ***ndn_list, **local_ndn_list, *ret;

	/* Supply an NDN list if the caller didn't. */
	if (visited_ndns != NULL) {
		ndn_list = visited_ndns;
	} else {
		ndn_list = &local_ndn_list;
		local_ndn_list = NULL;
	}

	/* Supply a PBlock if the caller didn't. */
	if (pb == NULL) {
		local_pb = slapi_pblock_new();
		pb = local_pb;
	} else {
		local_pb = NULL;
	}

	ret = format_format(state, pb, e, fmt, ndn_list);

	/* If we supplied a PBlock, clean it up. */
	if (pb == local_pb) {
		slapi_pblock_destroy(local_pb);
	}
	/* If we supplied an NDN list, clean it up. */
	if (ndn_list == &local_ndn_list) {
		format_free_ndn_list(local_ndn_list);
	}

	return ret;
}