summaryrefslogtreecommitdiffstats
path: root/src/dummymap.c
blob: d604258322d294c31181b9ed35b97c4f780503ca (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
/*
 * Copyright 2008 Red Hat, Inc.
 *
 * This Program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * 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.
 *   59 Temple Place, Suite 330
 *   Boston, MA 02111-1307 USA
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#ifdef HAVE_DIRSRV_SLAPI_PLUGIN_H
#include <nspr.h>
#include <nss.h>
#include <dirsrv/slapi-plugin.h>
#else
#include <slapi-plugin.h>
#endif

#include <rpc/rpc.h>

#include "disp-nis.h"
#include "map.h"
#include "nis.h"
#include "plugin.h"
#include "portmap.h"

struct entry {
	char *id, *key, *value;
};

struct map {
	char *map;
	struct entry *entries;
};

struct domain {
	char *domain;
	struct map *maps;
};

struct entry devel_passwd_byname[] = {
	{"a", "user1", "user1:*:1:1:User Number 1:/home/devel/user1:/bin/tcsh"},
	{"b", "user2", "user2:*:2:2:User Number 2:/home/devel/user2:/bin/tcsh"},
	{"c", "user3", "user3:*:3:3:User Number 3:/home/devel/user3:/bin/tcsh"},
	{"d", "user4", "user4:*:4:4:User Number 4:/home/devel/user4:/bin/tcsh"},
	{"e", "user5", "user5:*:5:5:User Number 5:/home/devel/user5:/bin/tcsh"},
	{NULL, NULL, NULL},
};

struct entry devel_passwd_bynumber[] = {
	{"a", "1", "user1:*:1:1:User Number 1:/home/devel/user1:/bin/tcsh"},
	{"b", "2", "user2:*:2:2:User Number 2:/home/devel/user2:/bin/tcsh"},
	{"c", "3", "user3:*:3:3:User Number 3:/home/devel/user3:/bin/tcsh"},
	{"d", "4", "user4:*:4:4:User Number 4:/home/devel/user4:/bin/tcsh"},
	{"e", "5", "user5:*:5:5:User Number 5:/home/devel/user5:/bin/tcsh"},
	{NULL, NULL, NULL},
};

struct map devel_maps[] = {
	{"passwd.byname", devel_passwd_byname},
	{"passwd.bynumber", devel_passwd_byname},
	{NULL, NULL},
};

struct domain domains[] = {
	{"devel.example.com", devel_maps},
	{NULL, NULL},
};

int
map_startup(struct plugin_state *state)
{
	return 0;
}

int
map_init(struct slapi_pblock *pb, struct plugin_state *state)
{
	return 0;
}

int
map_master_name(struct plugin_state *state, const char **master)
{
	*master = "me, right here";
	return 0;
}

bool_t
map_supports_domain(struct plugin_state *state,
		    const char *domain,
		    bool_t *supported)
{
	int i;
	if (supported != NULL) {
		*supported = FALSE;
		for (i = 0; domains[i].domain != NULL; i++) {
			if (strcmp(domains[i].domain, domain) == 0) {
				*supported = TRUE;
				break;
			}
		}
		slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
				"map_supports_domain \"%s\": %s\n", domain,
				*supported ? "YES" : "NO");
	}
	return TRUE;
}

bool_t
map_supports_map(struct plugin_state *state,
		 const char *domain, const char *map, bool_t *supported,
		 bool_t *secure)
{
	int i, j;
	*supported = FALSE;
	for (i = 0; domains[i].domain != NULL; i++) {
		if (strcmp(domains[i].domain, domain) == 0) {
			break;
		}
	}
	if (domains[i].domain != NULL) {
		for (j = 0; domains[i].maps[j].map != NULL; j++) {
			if (strcmp(domains[i].maps[j].map, map) == 0) {
				if (supported != NULL) {
					*supported = TRUE;
				}
				break;
			}
		}
	}
	if (secure != NULL) {
		*secure = FALSE;
	}
	return TRUE;
}

bool_t
map_order(struct plugin_state *state,
	  const char *domain, const char *map, bool_t *secure,
	  unsigned int *order)
{
	*order = 1;
	*secure = FALSE;
	return TRUE;
}

static struct entry *
map_find_entries(const char *domain, const char *map)
{
	int i, j;
	for (i = 0; domains[i].domain != NULL; i++) {
		if (strcmp(domains[i].domain, domain) == 0) {
			break;
		}
	}
	if (domains[i].domain == NULL) {
		return NULL;
	}
	for (j = 0; domains[i].maps[j].map != NULL; j++) {
		if (strcmp(domains[i].maps[j].map, map) == 0) {
			break;
		}
	}
	if (domains[i].maps[j].map == NULL) {
		return NULL;
	}
	return domains[i].maps[j].entries;
}

bool_t
map_match(struct plugin_state *state,
	  const char *domain, const char *map, bool_t *secure,
	  unsigned int key_len, const char *key,
	  unsigned int *value_len, const char **value,
	  const char **id, void **backend_data)
{
	struct entry *entries;
	int i;
	if ((entries = map_find_entries(domain, map)) == NULL) {
		return FALSE;
	}
	for (i = 0; entries[i].key != NULL; i++) {
		if ((strlen(entries[i].key) == key_len) &&
		    (memcmp(entries[i].key, key, key_len) == 0)) {
			break;
		}
	}
	if (entries[i].key == NULL) {
		return FALSE;
	}
	*secure = FALSE;
	*value = entries[i].value;
	*value_len = strlen(entries[i].value);
	*id = entries[i].key;
	if (backend_data != NULL) {
		*backend_data = NULL;
	}
	return TRUE;
}

bool_t
map_match_id(struct plugin_state *state,
	     const char *domain, const char *map, bool_t *secure,
	     const char *in_id, unsigned int in_index,
	     unsigned int *key_len, const char **key,
	     unsigned int *value_len, const char **value,
	     const char **id, void **backend_data)
{
	struct entry *entries;
	int i;
	if (in_index != 0) {
		return FALSE;
	}
	if ((entries = map_find_entries(domain, map)) == NULL) {
		return FALSE;
	}
	for (i = 0; entries[i].id != NULL; i++) {
		if (strcmp(entries[i].id, in_id) == 0) {
			break;
		}
	}
	if (entries[i].id == NULL) {
		return FALSE;
	}
	*secure = FALSE;
	*key = entries[i].key;
	*key_len = strlen(entries[i].key);
	*value = entries[i].value;
	*value_len = strlen(entries[i].value);
	*id = entries[i].id;
	if (backend_data != NULL) {
		*backend_data = NULL;
	}
	return TRUE;
}

bool_t
map_first(struct plugin_state *state,
	  const char *domain, const char *map, bool_t *secure,
	  unsigned int *first_key_len, char **first_key,
	  unsigned int *first_value_len, char **first_value,
	  const char **first_id, int *first_id_index)
{
	struct entry *entries;
	if ((entries = map_find_entries(domain, map)) == NULL) {
		return FALSE;
	}
	if (entries[0].key == NULL) {
		return FALSE;
	}
	*secure = FALSE;
	*first_key = entries[0].key;
	*first_key_len = strlen(entries[0].key);
	*first_value = entries[0].value;
	*first_value_len = strlen(entries[0].value);
	*first_id = entries[0].id;
	*first_id_index = 0;
	return TRUE;
}

bool_t
map_next(struct plugin_state *state,
	 const char *domain, const char *map, bool_t *secure,
	 unsigned int prev_len, const char *prev,
	 unsigned int *next_key_len, char **next_key,
	 unsigned int *next_value_len, char **next_value)
{
	struct entry *entries;
	int i;
	if ((entries = map_find_entries(domain, map)) == NULL) {
		return FALSE;
	}
	for (i = 0; entries[i].key != NULL; i++) {
		if ((strlen(entries[i].key) == prev_len) &&
		    (memcmp(entries[i].key, prev, prev_len) == 0)) {
			break;
		}
	}
	if (entries[i].key == NULL) {
		return FALSE;
	}
	if (entries[i + 1].key == NULL) {
		return FALSE;
	}
	*secure = FALSE;
	*next_key = entries[i + 1].key;
	*next_key_len = strlen(entries[i + 1].key);
	*next_value = entries[i + 1].value;
	*next_value_len = strlen(entries[i + 1].value);
	return TRUE;
}

bool_t
map_next_id(struct plugin_state *state,
	    const char *domain, const char *map, bool_t *secure,
	    const char *prev_id, int prev_id_index,
	    unsigned int *next_key_len, char **next_key,
	    unsigned int *next_value_len, char **next_value,
	    const char **next_id, int *next_id_index)
{
	struct entry *entries;
	int i;
	if ((entries = map_find_entries(domain, map)) == NULL) {
		return FALSE;
	}
	for (i = 0; entries[i].id != NULL; i++) {
		if (strcmp(entries[i].id, prev_id) == 0) {
			break;
		}
	}
	if (entries[i].id == NULL) {
		return FALSE;
	}
	if (entries[i + 1].id == NULL) {
		return FALSE;
	}
	*secure = FALSE;
	*next_key = entries[i + 1].key;
	*next_key_len = strlen(entries[i + 1].key);
	*next_value = entries[i + 1].value;
	*next_value_len = strlen(entries[i + 1].value);
	*next_id = entries[i + 1].id;
	*next_id_index = 0;
	return TRUE;
}