summaryrefslogtreecommitdiffstats
path: root/src/map.c
blob: 2d23a990acf54f4760d4d786d645c8a32cf8bd1e (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
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif

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

#include <rpc/rpc.h>
#include <rpcsvc/yp_prot.h>

#include <nspr.h>
#include <secport.h>
#include <plarenas.h>
#include <dirsrv/slapi-plugin.h>

#include "dispatch.h"
#include "map.h"
#include "nis.h"
#include "plugin.h"
#include "portmap.h"

struct {
	struct domain {
		char *name;
		struct map {
			char *name;
			char *search_base;
			int search_scope;
			char *entry_filter;
			char *key_attribute;
			char *value_format;
			time_t last_changed;
			struct entry {
				struct entry *prev, *next;
				char *dn;
				char *key;
				unsigned int key_len;
				char *value;
				unsigned int value_len;
			} *entries;
		} *maps;
		int n_maps;
	} *domains;
	int n_domains;
} map_data;

static struct domain *
map_find_domain(struct plugin_state *state, const char *domain_name)
{
	int i;
	for (i = 0; i < map_data.n_domains; i++) {
		if (strcmp(domain_name, map_data.domains[i].name) == 0) {
			return &map_data.domains[i];
		}
	}
	return NULL;
}

static struct map *
map_find_map(struct plugin_state *state,
	     const char *domain_name, const char *map_name)
{
	int i;
	struct domain *domain;
	domain = map_find_domain(state, domain_name);
	if (domain != NULL) {
		for (i = 0; i < domain->n_maps; i++) {
			if (strcmp(map_name, domain->maps[i].name) == 0) {
				return &domain->maps[i];
			}
		}
	}
	return NULL;
}

bool_t
map_supports_domain(struct plugin_state *state,
		    const char *domain_name,
		    bool_t *supported)
{
	*supported = (map_find_domain(state, domain_name) != NULL);
	return TRUE;
}

bool_t
map_supports_map(struct plugin_state *state,
		 const char *domain_name, const char *map_name,
		 bool_t *supported)
{
	*supported = (map_find_map(state, domain_name, map_name) != NULL);
	return TRUE;
}

bool_t
map_order(struct plugin_state *state,
	  const char *domain_name, const char *map_name,
	  unsigned int *order)
{
	struct map *map;
	map = map_find_map(state, domain_name, map_name);
	if (map != NULL) {
		*order = map->last_changed & 0xffffffff;
		return TRUE;
	} else {
		return FALSE;
	}
}

bool_t
map_match(struct plugin_state *state,
	  const char *domain_name, const char *map_name,
	  unsigned int key_len, char *key,
	  unsigned int *value_len, char **value)
{
	struct map *map;
	struct entry *entry;
	map = map_find_map(state, domain_name, map_name);
	if (map == NULL) {
		return FALSE;
	}
	for (entry = map->entries; entry != NULL; entry = entry->next) {
		if ((entry->key_len == key_len) &&
		    (memcmp(entry->key, key, key_len) == 0)) {
			*value_len = entry->value_len;
			*value = entry->value;
			return TRUE;
		}
	}
	return FALSE;
}

bool_t
map_first(struct plugin_state *state,
	  const char *domain_name, const char *map_name,
	  unsigned int *first_key_len, char **first_key,
	  unsigned int *first_value_len, char **first_value)
{
	struct map *map;
	struct entry *entry;
	map = map_find_map(state, domain_name, map_name);
	if (map == NULL) {
		return FALSE;
	}
	entry = map->entries;
	if (entry == NULL) {
		return FALSE;
	}
	*first_key_len = entry->key_len;
	*first_key = entry->key;
	*first_value_len = entry->value_len;
	*first_value = entry->value;
	return TRUE;
}

bool_t
map_next(struct plugin_state *state,
	 const char *domain_name, const char *map_name,
	 unsigned int prev_len, const char *prev,
	 unsigned int *next_key_len, char **next_key,
	 unsigned int *next_value_len, char **next_value)
{
	struct map *map;
	struct entry *entry;
	map = map_find_map(state, domain_name, map_name);
	if (map == NULL) {
		return FALSE;
	}
	for (entry = map->entries; entry != NULL; entry = entry->next) {
		if ((entry->key_len == prev_len) &&
		    (memcmp(entry->key, prev, prev_len) == 0)) {
			entry = entry->next;
			if (entry == NULL) {
				return FALSE;
			}
			*next_key_len = entry->key_len;
			*next_key = entry->key;
			*next_value_len = entry->value_len;
			*next_value = entry->value;
			return TRUE;
		}
	}
	return FALSE;
}