File: xml-parse.c

package info (click to toggle)
debsig-verify 0.10
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 152 kB
  • ctags: 96
  • sloc: ansic: 814; makefile: 84
file content (290 lines) | stat: -rw-r--r-- 8,549 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
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
/*
 * debsig-verify - Debian package signature verification tool
 *
 * Copyright © 2000 Ben Collins <bcollins@debian.org>
 *
 * This 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This 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, see <https://www.gnu.org/licenses/>.
 */

/*
 * provides the XML parsing code for policy files, via expat (xmltok)
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include <sys/stat.h>
#include <obstack.h>

#include <xmltok/xmlparse.h>

#include "debsig.h"

#define obstack_chunk_alloc xmalloc
#define obstack_chunk_free free

static int parse_err_cnt;
static struct policy ret;
static struct group *cur_grp = NULL;
static XML_Parser parser;
static struct obstack deb_obs;
static int deb_obs_init = 0;

#define parse_error(fmt, args...) \
{ \
    parse_err_cnt++; \
    ds_printf(DS_LEV_DEBUG , "%d: " fmt , XML_GetCurrentLineNumber(parser) , ## args); \
}

static void *xmalloc(size_t size) {
    register void *p = malloc(size);
    if (p == NULL)
	ds_fail_printf(DS_FAIL_INTERNAL, "out of memory");
    return p;
}

static void startElement(void *userData, const char *name, const char **atts) {
    int i, depth;
    int *depthPtr = userData;

    /* save the current and increment the userdata */
    depth = *depthPtr;
    *depthPtr = depth + 1;

    if (!strcmp(name,"Policy")) {
	if (depth != 0)
	    parse_error("policy parse error: 'Policy' found at wrong level");

	for (i = 0; atts[i]; i += 2) {
	    if (!strcmp(atts[i], "xmlns")) {
		if (strcmp(atts[i + 1], DEBSIG_NAMESPACE))
		    parse_error("policy name space != " DEBSIG_NAMESPACE);
	    } else
		parse_error("Policy element contains unknown attribute '%s'",
			     atts[i]);
	}
    } else if (!strcmp(name,"Origin")) {
	if (depth != 1)
	    parse_error("policy parse error: 'Origin' found at wrong level");
	
	for (i = 0; atts[i]; i += 2) {
	    if (!strcmp(atts[i], "id"))
		ret.id = obstack_copy0(&deb_obs, atts[i+1], strlen(atts[i+1]));
	    else if (!strcmp(atts[i], "Name"))
		ret.name = obstack_copy0(&deb_obs, atts[i+1], strlen(atts[i+1]));
	    else if (!strcmp(atts[i], "Description"))
		ret.description = obstack_copy0(&deb_obs, atts[i+1], strlen(atts[i+1]));
	    else
		parse_error("Origin element contains unknown attribute '%s'",
			     atts[i]);
	}

	if (ret.id == NULL || ret.name == NULL)
	    parse_error("Origin element missing Name or ID attribute");
    } else if (!strcmp(name,"Selection") || !strcmp(name,"Verification")) {
	struct group *g = NULL;
	if (depth != 1)
	    parse_error("policy parse error: 'Selection/Verification' found at wrong level");

	/* create a new entry, make it the current */
	cur_grp = (struct group *)obstack_alloc(&deb_obs, sizeof(struct group));
	if (cur_grp == NULL)
	    ds_fail_printf(DS_FAIL_INTERNAL, "out of memory");
	memset(cur_grp, 0, sizeof(struct group));

	if (!strcmp(name,"Selection")) {
	    if (ret.sels == NULL)
		ret.sels = cur_grp;
	    else
		g = ret.sels;
	} else {
	    if (ret.vers == NULL)
		ret.vers = cur_grp;
	    else
		g = ret.vers;
	}
	if (g) {
	    for ( ; g->next; g = g->next)
		; /* find the end of the chain */
	    g->next = cur_grp;
	}

	for (i = 0; atts[i]; i += 2) {
	    if (!strcmp(atts[i], "MinOptional")) {
		int t; const char *c = atts[i+1];
		for (t = 0; c[t]; t++) {
		    if (!isdigit(c[t]))
			parse_error("MinOptional requires a numerical value");
		}
		cur_grp->min_opt = atoi(c);
	    } else {
		parse_error("Selection/Verification element contains unknown attribute '%s'",
			     atts[i]);
	    }
	}
    } else if (!strcmp(name,"Required") || !strcmp(name,"Reject") ||
	       !strcmp(name,"Optional")) {
	struct match *m = NULL, *cur_m = NULL;
	if (depth != 2)
	    parse_error("policy parse error: Match element found at wrong level");

	/* This should never happen with the other checks in place */
	if (cur_grp == NULL) {
	    parse_error("policy parse error: No current group for match element");
	    return;
	}

        /* create a new entry, make it the current */
        cur_m = (struct match *)obstack_alloc(&deb_obs, sizeof(struct match));
        if (cur_m == NULL)
            ds_fail_printf(DS_FAIL_INTERNAL, "out of memory");
        memset(cur_m, 0, sizeof(struct match));

	if (cur_grp->matches == NULL)
	    cur_grp->matches = cur_m;
	else {
	    for (m = cur_grp->matches; m->next; m = m->next)
		; /* find the end of the chain */
	    m->next = cur_m;
	}

	/* Set the attributes first, so we can sanity check the type after */
        for (i = 0; atts[i]; i += 2) {
            if (!strcmp(atts[i], "Type")) {
                cur_m->name = obstack_copy0(&deb_obs, atts[i+1], strlen(atts[i+1]));
	    } else if (!strcmp(atts[i], "File")) {
		cur_m->file = obstack_copy0(&deb_obs, atts[i+1], strlen(atts[i+1]));;
	    } else if (!strcmp(atts[i], "id")) {
		cur_m->id = obstack_copy0(&deb_obs, atts[i+1], strlen(atts[i+1]));;
	    } else if (!strcmp(atts[i], "Expiry")) {
		int t; const char *c = atts[i+1];
		for (t = 0; c[t]; t++) {
		    if (!isdigit(c[t]))
			parse_error("Expiry requires a numerical value");
		}
                cur_m->day_expiry = atoi(c);
            } else {
                parse_error("Match element contains unknown attribute '%s'",
                             atts[i]);
            }
        }


        if (!strcmp(name,"Required")) {
	    cur_m->type = REQUIRED_MATCH;
	    if (cur_m->name == NULL || cur_m->file == NULL)
		parse_error("Required must have a Type and File attribute");
	} else if (!strcmp(name,"Optional")) {
	    cur_m->type = OPTIONAL_MATCH;
	    if (cur_m->name == NULL || cur_m->file == NULL)
		parse_error("Optional must have a Type and File attribute");
	} else { /* Reject */
	    cur_m->type = REJECT_MATCH;
	    if (cur_m->name == NULL)
		parse_error("Reject must have a Type attribute");
	}
    }
}

static void endElement(void *userData, const char *name) {
    int *depthPtr = userData;
    *depthPtr -= 1;

    if (!strcmp(name,"Selection") || !strcmp(name,"Verification")) {
	struct match *m; int i = 0;
	/* sanity check this block */
	for (m = cur_grp->matches; m; m = m->next) {
	    if (m->type == OPTIONAL_MATCH ||
		m->type == REQUIRED_MATCH)
		i++;
	}
	if (!i) {
	    parse_error("Selection/Verification block does not contain any "
			 "Required or Optional matches.");
	}
	cur_grp = NULL; /* just to make sure */
    }
}

void clear_policy(void) {
    if (deb_obs_init) {
	obstack_free(&deb_obs, 0);
	deb_obs_init = 0;
    }
    memset(&ret, '\0', sizeof(struct policy));
    return;
}

struct policy *parsePolicyFile(const char *filename) {
    char buf[BUFSIZ];
    int done, depth = 0;
    FILE *pol_fs;
    struct stat st;

    /* clear and initialize */
    parser = XML_ParserCreate(NULL);
    //clear_policy();
    obstack_init(&deb_obs);
    deb_obs_init = 1;

    ds_printf(DS_LEV_DEBUG, "    parsePolicyFile: parsing '%s'", filename);

    if (stat(filename, &st)) {
	ds_printf(DS_LEV_ERR, "parsePolicyFile: could not stat %s", filename);
	return NULL;
    }
    if (!S_ISREG(st.st_mode)) {
	ds_printf(DS_LEV_ERR, "parsePolicyFile: %s is not a regular file", filename);
	return NULL;
    }
    if ((pol_fs = fopen(filename, "r")) == NULL) {
	ds_printf(DS_LEV_ERR, "parsePolicyFile: could not open '%s' (%s)",
		  filename, strerror(errno));
	return NULL;
    }

    XML_SetUserData(parser, &depth);
    XML_SetElementHandler(parser, startElement, endElement);

    parse_err_cnt = 0;

    do {
	size_t len = fread(buf, 1, sizeof(buf), pol_fs);
	done = len < sizeof(buf);
	if (!XML_Parse(parser, buf, len, done)) {
	    ds_printf(DS_LEV_DEBUG,
		"%s at line %d",
		XML_ErrorString(XML_GetErrorCode(parser)),
		XML_GetCurrentLineNumber(parser));
	    parse_err_cnt++;
	    break;
	}
    } while (!done);

    XML_ParserFree(parser);
    fclose(pol_fs);

    ds_printf(DS_LEV_DEBUG, "    parsePolicyFile: completed");

    if (parse_err_cnt) {
	ds_printf(DS_LEV_DEBUG, "    parsePolicyFile: %d errors during parsing, failed",
		  parse_err_cnt);
	clear_policy();
	return NULL;
    }

    return &ret;
}