summaryrefslogtreecommitdiff
path: root/payloads/bayou/util/pbuilder/config.c
blob: a1d49fb69c508f4e55a5982607e753b79ab508d6 (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
/*
 * This file is part of the bayou project.
 *
 * Copyright (C) 2008 Advanced Micro Devices, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

#define _GNU_SOURCE

#include <stdlib.h>
#include <string.h>
#include <expat.h>
#include <ctype.h>
#include "pbuilder.h"

#define STATE_NONE                   0x00
#define STATE_BAYOUCONFIG            0x01
#define STATE_GLOBAL                 0x02
#define STATE_GLOBAL_TIMEOUT         0x03
#define STATE_PAYLOADS               0x04
#define STATE_PAYLOAD                0x05
#define STATE_PAYLOAD_TITLE          0x06
#define STATE_PAYLOAD_FILE           0x07
#define STATE_PAYLOAD_LARNAME        0x08
#define STATE_PAYLOAD_CHAIN          0x09
#define STATE_PAYLOAD_CHAIN_FILE     0x0A
#define STATE_PAYLOAD_CHAIN_LARNAME  0x0B

static struct userdata {
	struct config *config;
	struct pentry *payload;
	struct pentry *chain;
	int state;
} userdata;

static char buffer[8192];

static struct {
	char *element;
	int pstate;
	int state;
} states[] = {
	{"BayouConfig", STATE_NONE, STATE_BAYOUCONFIG},
	{"global", STATE_BAYOUCONFIG, STATE_GLOBAL},
	{"timeout", STATE_GLOBAL, STATE_GLOBAL_TIMEOUT},
	{"payloads", STATE_BAYOUCONFIG, STATE_PAYLOADS},
	{"payload", STATE_PAYLOADS, STATE_PAYLOAD},
	{"title", STATE_PAYLOAD, STATE_PAYLOAD_TITLE},
	{"lar", STATE_PAYLOAD, STATE_PAYLOAD_LARNAME},
	{"file", STATE_PAYLOAD, STATE_PAYLOAD_FILE},
	{"chain", STATE_PAYLOAD, STATE_PAYLOAD_CHAIN},
	{"file", STATE_PAYLOAD_CHAIN, STATE_PAYLOAD_CHAIN_FILE},
	{"lar", STATE_PAYLOAD_CHAIN, STATE_PAYLOAD_CHAIN_LARNAME},
	{NULL},
};

#ifndef __LINUX__
static char *strndup (const char *s, size_t n)
{
	size_t len = strlen (s);
	len = (len<n)?len:n;
	char *cpy = malloc (len + 1);
	if (cpy == NULL)
		return NULL;
	cpy[len] = '\0';
	memcpy (cpy, s, len);
	return cpy;
}
#endif

static struct pentry *newPayload(struct config *config)
{
	struct pentry **tmp, *ret;

	tmp = realloc(config->entries,
		      (config->n_entries + 1) * sizeof(struct pentry *));

	if (tmp == NULL)
		return NULL;

	config->entries = tmp;

	ret = config->entries[config->n_entries] = calloc(sizeof(*ret), 1);

	if (ret == NULL)
		return NULL;

	/* Yes, I want this to be ++config->n_entries (the index is 1 based). */
	ret->index = ++config->n_entries;

	return ret;
}

static void parseFlags(struct pentry *entry, const char *flags)
{
	char *p = (char *)flags;
	char *n;

	while (*p) {
		n = strchr(p, ',');

		if (n)
			*(n++) = 0;

		if (!strcmp(p, "default"))
			entry->flags |= BPT_FLAG_DEFAULT;
		else if (!strcmp(p, "nolist"))
			entry->flags |= BPT_FLAG_NOSHOW;
		else
			warn("W: Unknown payload flag %s\n", p);

		if (!n)
			break;

		for (p = n; *p && isspace(*p); p++) ;
	}
}

static struct pentry *addPayload(struct config *config, const char **attr)
{
	struct pentry *ret = newPayload(config);
	int i = 0;

	if (ret == NULL)
		die("E: Could not allocate memory for a new payload\n");

	while (attr[i] != NULL) {
		if (!strcmp(attr[i], "type")) {
			if (!strcmp(attr[i + 1], "chain"))
				ret->type = BPT_TYPE_CHAIN;
			else if (!strcmp(attr[i + 1], "chooser"))
				ret->type = BPT_TYPE_CHOOSER;
			else
				die("E: Invalid payload type %s\n",
				    attr[i + 1]);
		} else if (!strcmp(attr[i], "flags"))
			parseFlags(ret, attr[i + 1]);

		i += 2;
	}

	return ret;
}

static struct pentry *addChain(struct config *config, struct pentry *parent)
{
	struct pentry *ret = newPayload(config);

	if (ret == NULL)
		die("E: Could not allocate memory for a new payload\n");

	ret->parent = parent->index;
	ret->type = BPT_TYPE_SUBCHAIN;

	return ret;
}

static void start(void *data, const char *el, const char **attr)
{
	int i;
	struct userdata *d = (struct userdata *)data;

	for (i = 0; states[i].element != NULL; i++) {
		if (d->state != states[i].pstate)
			continue;

		if (!strcmp(el, states[i].element)) {
			d->state = states[i].state;
			break;
		}
	}

	if (states[i].element == NULL)
		die("E: Unknown element %s\n", el);

	if (d->state == STATE_PAYLOAD)
		d->payload = addPayload(d->config, attr);

	if (d->state == STATE_PAYLOAD_CHAIN)
		d->chain = addChain(d->config, d->payload);
}

static void data(void *data, const char *val, int len)
{
	struct userdata *d = (struct userdata *)data;
	int l;

	switch (d->state) {
	case STATE_GLOBAL_TIMEOUT:
		d->config->timeout = atoi(val);
		break;
	case STATE_PAYLOAD_TITLE:
		l = sizeof(d->payload->title) - 1 > len ?
		    len : sizeof(d->payload->title) - 1;

		strncpy((char *)d->payload->title, (char *)val, l);
		d->payload->title[l] = '\0';
		break;
	case STATE_PAYLOAD_FILE:
		d->payload->file = strndup(val, len);
		break;
	case STATE_PAYLOAD_LARNAME:
		d->payload->larname = strndup(val, len);
		break;
	case STATE_PAYLOAD_CHAIN_FILE:
		d->chain->file = strndup(val, len);
		break;
	case STATE_PAYLOAD_CHAIN_LARNAME:
		d->chain->larname = strndup(val, len);
		break;
	default:
		break;
	}
}

static void end(void *data, const char *el)
{
	struct userdata *d = (struct userdata *)data;
	int i;

	if (d->state == STATE_PAYLOAD_CHAIN)
		d->chain = NULL;

	if (d->state == STATE_PAYLOAD)
		d->payload = NULL;

	for (i = 0; states[i].element != NULL; i++) {
		if (d->state != states[i].state)
			continue;

		if (!strcmp(el, states[i].element)) {
			d->state = states[i].pstate;
			break;
		}
	}

	if (states[i].element == NULL)
		die("E:  Unable to find the reverse state for %s\n", el);
}

void parseconfig(FILE *stream, struct config *config)
{
	XML_Parser p = XML_ParserCreate(NULL);
	int eof = 0;

	if (p == NULL)
		die("E: Could not create the parser\n");

	XML_SetElementHandler(p, start, end);
	XML_SetCharacterDataHandler(p, data);

	userdata.config = config;

	XML_SetUserData(p, &userdata);

	while (!eof) {
		int len = fread(buffer, 1, 8192, stream);

		eof = feof(stream);

		if (ferror(stream))
			die("E: Error reading the stream\n");

		if (!XML_Parse(p, buffer, len, eof))
			die("E: Error parsing the XML data\n");
	}
}