File: static_map.c

package info (click to toggle)
fence-virt 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 772 kB
  • sloc: ansic: 10,257; cpp: 382; makefile: 272; sh: 214; lex: 113; yacc: 105
file content (226 lines) | stat: -rw-r--r-- 4,057 bytes parent folder | download
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
#include "config.h"

#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>

#include "simpleconfig.h"
#include "static_map.h"
#include "list.h"
#include "debug.h"
#include "serial.h"
#include "uuid-test.h"

struct perm_entry {
	list_head();
	char name[128];
};

struct perm_group {
	list_head();
	struct perm_entry *uuids;
	struct perm_entry *ips;
	char name[128];
};


static void
static_map_cleanup(void **info)
{
	struct perm_group *groups = (struct perm_group *)(*info);
	struct perm_group *group;
	struct perm_entry *entry;

	while (groups) {
		group = groups;
		list_remove(&groups, group);
		while (group->uuids) {
			entry = group->uuids;
			list_remove(&group->uuids, entry);
			free(entry);
		}
		while (group->ips) {
			entry = group->ips;
			list_remove(&group->ips, entry);
			free(entry);
		}
		free(group);
	}

	*info = NULL;
}


static int
static_map_check(void *info, const char *value1, const char *value2)
{
	struct perm_group *groups = (struct perm_group *)info;
	struct perm_group *group;
	struct perm_entry *left, *tmp;
	int x, y, uuid = 0;

	if (!info)
		return 1; /* no maps == wide open */

	uuid = is_uuid(value1);

	list_for(&groups, group, x) {
		left = NULL;

		if (uuid) {
			list_for(&group->uuids, tmp, y) {
				if (!strcasecmp(tmp->name, value1)) {
					left = tmp;
					break;
				}
			}
		} else {
			list_for(&group->ips, tmp, y) {
				if (!strcasecmp(tmp->name, value1)) {
					left = tmp;
					break;
				}
			}
		}

		if (!left)
			continue;

		list_for(&group->uuids, tmp, y) {
			if (!strcasecmp(tmp->name, value2)) {
				return 1;
			}
		}
	}

	return 0;
}


static int
static_map_load(void *config_ptr, void **perm_info)
{
	config_object_t *config = config_ptr;
	int group_idx = 0;
	int entry_idx = 0;
	int found;
	char value[128];
	char buf[256];
	char buf2[512];
	struct perm_group *group = NULL, *groups = NULL;
	struct perm_entry *entry = NULL;

	if (!perm_info)
		return -1;

	do {
		snprintf(buf, sizeof(buf)-1, "groups/group[%d]", ++group_idx);

		if (sc_get(config, buf, value, sizeof(value)) != 0) {
			snprintf(buf2, sizeof(buf2)-1, "%s/@uuid", buf);
			if (sc_get(config, buf2, value, sizeof(value)) != 0) {
				snprintf(buf2, sizeof(buf2)-1, "%s/@ip", buf);
				if (sc_get(config, buf2, value,
					   sizeof(value)) != 0) {
					break;
				}
			}
			snprintf(value, sizeof(value), "unnamed-%d",
				 group_idx);
		}

		group = malloc(sizeof(*group));
		assert(group);
		memset(group, 0, sizeof(*group));
		strncpy(group->name, value, sizeof(group->name));
		dbg_printf(3, "Group: %s\n", value);

		found = 0;
		entry_idx = 0;
		do {
			snprintf(buf2, sizeof(buf2)-1, "%s/@uuid[%d]",
				 buf, ++entry_idx);

			if (sc_get(config, buf2, value, sizeof(value)) != 0) {
				break;
			}

			++found;
			entry = malloc(sizeof(*entry));
			assert(entry);
			memset(entry, 0, sizeof(*entry));
			strncpy(entry->name, value, sizeof(entry->name));
			dbg_printf(3, " - UUID Entry: %s\n", value);

			list_insert(&group->uuids, entry);

		} while (1);

		entry_idx = 0;
		do {
			snprintf(buf2, sizeof(buf2)-1, "%s/@ip[%d]",
				 buf, ++entry_idx);

			if (sc_get(config, buf2, value, sizeof(value)) != 0) {
				break;
			}

			++found;
			entry = malloc(sizeof(*entry));
			assert(entry);
			memset(entry, 0, sizeof(*entry));
			strncpy(entry->name, value, sizeof(entry->name));
			dbg_printf(3, " - IP Entry: %s\n", value);

			list_insert(&group->ips, entry);

		} while (1);


		if (!found)
			free(group);
		else
			list_insert(&groups, group);

	} while (1);

	*perm_info = groups;

	return 0;
}


static const map_object_t static_map_obj = {
	.load = static_map_load,
	.check = static_map_check,
	.cleanup = static_map_cleanup,
	.info = NULL
};


void *
map_init(void)
{
	map_object_t *o;

	o = malloc(sizeof(*o));
	if (!o)
		return NULL;
	memset(o, 0, sizeof(*o));
	memcpy(o, &static_map_obj, sizeof(*o));

	return (void *)o;
}


void
map_release(void *c)
{
	map_object_t *o = (map_object_t *)c;

	static_map_cleanup(&o->info);
	free(c);
}