summaryrefslogtreecommitdiffstats
path: root/server/parser/xmlparser.c
blob: 281ab7fa736ab040421c89c146cafd0cf67bf7d4 (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
/*
 * Copyright (C) 2009 Red Hat Inc.
 *
 * David Sommerseth <davids@redhat.com>
 *
 * Parses summary.xml reports from rteval into a standardised XML format
 * which is useful when putting data into a database.
 *
 * This application 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.
 *
 * This application 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.
 */

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

#include <libxml/tree.h>
#include <libxslt/xsltInternals.h>
#include <libxslt/transform.h>
#include <libxslt/xsltutils.h>

#include <eurephia_nullsafe.h>
#include <eurephia_xml.h>
#include <xmlparser.h>
#include <sha1.h>


static char *encapsString(const char *str) {
        char *ret = NULL;

        if( str == NULL ) {
                return NULL;
        }

        ret = (char *) calloc(1, strlen(str)+4);
        assert( ret != NULL );

        snprintf(ret, strlen(str)+3, "'%s'", str);
        return ret;
}


static char *encapsInt(const unsigned int val) {
        char *buf = NULL;

        buf = (char *) calloc(1, 130);
        snprintf(buf, 128, "'%i'", val);
        return buf;
}


xmlDoc *parseToSQLdata(xsltStylesheet *xslt, xmlDoc *indata_d, parseParams *params) {
        xmlDoc *result_d = NULL;
        char *xsltparams[10];
        unsigned int idx = 0, idx_table = 0, idx_syskey = 0, idx_rterid = 0, idx_repfname = 0;

        if( params->table == NULL ) {
                fprintf(stderr, "Table is not defined\n");
                return NULL;
        }

        // Prepare XSLT parameters
        xsltparams[idx++] = "table\0";
        xsltparams[idx] = (char *) encapsString(params->table);
        idx_table = idx++;

        if( params->syskey > 0) {
                xsltparams[idx++] = "syskey\0";
                xsltparams[idx] = (char *) encapsInt(params->syskey);
                idx_syskey = idx++;
        }

        if( params->rterid > 0 ) {
                xsltparams[idx++] = "rterid";
                xsltparams[idx] = (char *) encapsInt(params->rterid);
                idx_rterid = idx++;
        }

        if( params->report_filename ) {
                xsltparams[idx++] = "report_filename";
                xsltparams[idx] = (char *) encapsString(params->report_filename);
                idx_repfname = idx++;
        }
        xsltparams[idx] = NULL;

        // Apply the XSLT template to the input XML data
        result_d = xsltApplyStylesheet(xslt, indata_d, (const char **)xsltparams);
        if( result_d == NULL ) {
                fprintf(stderr, "Failed applying XSLT template to input XML\n");
        }

        // Free memory we allocated via encapsString()/encapsInt()
        free(xsltparams[idx_table]);
        if( params->syskey ) {
                free(xsltparams[idx_syskey]);
        }
        if( params->rterid ) {
                free(xsltparams[idx_rterid]);
        }
        if( params->report_filename ) {
                free(xsltparams[idx_repfname]);
        }

        return result_d;
}


char *sqldataValueHash(xmlNode *sql_n) {
	const char *hash = NULL;
	SHA1Context shactx;
	uint8_t shahash[SHA1_HASH_SIZE];
	char *ret = NULL, *ptr = NULL;;
	int i;

	if( !sql_n || (xmlStrcmp(sql_n->name, (xmlChar *) "value") != 0)
	    || (xmlStrcmp(sql_n->parent->name, (xmlChar *) "record") != 0) ) {
		    return NULL;
	}

	hash = xmlGetAttrValue(sql_n->properties, "hash");
	if( !hash ) {
		// If no hash attribute is found, just use the raw data
		ret = xmlExtractContent(sql_n);
	} else if( strcasecmp(hash, "sha1") == 0 ) {
		const char *indata = xmlExtractContent(sql_n);
		// SHA1 hashing requested
		SHA1Init(&shactx);
		SHA1Update(&shactx, indata, strlen_nullsafe(indata));
		SHA1Final(&shactx, shahash);

		// "Convert" to a readable format
		ret = malloc_nullsafe((SHA1_HASH_SIZE * 2) + 3);
		ptr = ret;
		for( i = 0; i < SHA1_HASH_SIZE; i++ ) {
			sprintf(ptr, "%02x", shahash[i]);
			ptr += 2;
		}
	} else {
		ret = strdup("<Unsupported hashing algorithm>");
	}

	return ret;
}


char *sqldataExtractContent(xmlNode *sql_n) {
	const char *valtype = xmlGetAttrValue(sql_n->properties, "type");

	if( !sql_n || (xmlStrcmp(sql_n->name, (xmlChar *) "value") != 0)
	    || (xmlStrcmp(sql_n->parent->name, (xmlChar *) "record") != 0) ) {
		    return NULL;
	}

	if( valtype && (strcmp(valtype, "xmlblob") == 0) ) {
		xmlNode *chld_n = sql_n->children;

		// Go to next "real" tag, skipping non-element nodes
		while( chld_n && chld_n->type != XML_ELEMENT_NODE ){
			chld_n = chld_n->next;
		}
		return xmlNodeToString(chld_n);
	} else {
		return sqldataValueHash(sql_n);
	}
}


int sqldataGetFid(xmlDoc *sqld, const char *fname) {
	xmlNode *f_n = NULL;

	f_n = xmlDocGetRootElement(sqld);
	if( !f_n || (xmlStrcmp(f_n->name, (xmlChar *) "sqldata") != 0) ) {
		fprintf(stderr, "** ERROR ** Input XML document is not a valid sqldata document\n");
		return -2;
	}

	f_n = xmlFindNode(f_n, "fields");
	if( !f_n || !f_n->children ) {
		fprintf(stderr, "** ERROR ** Input XML document does not contain a fields section\n");
		return -2;
	}

	foreach_xmlnode(f_n->children, f_n) {
		if( (f_n->type != XML_ELEMENT_NODE)
		    || xmlStrcmp(f_n->name, (xmlChar *) "field") != 0 ) {
			// Skip uninteresting nodes
			continue;
		}

		if( strcmp(xmlExtractContent(f_n), fname) == 0 ) {
			char *fid = xmlGetAttrValue(f_n->properties, "fid");
			if( !fid ) {
				fprintf(stderr, "** ERROR ** Field node is missing 'fid' attribute\n");
				return -2;
			}
			return atoi_nullsafe(fid);
		}
	}
	return -1;
}


char *sqldataGetValue(xmlDoc *sqld, int fid, int recid ) {
	xmlNode *r_n = NULL;
	int rc = 0;

	if( (fid < 0) || (recid < 0) ) {
		fprintf(stderr, "** ERROR ** sqldataGetValue() :: Invalid fid or recid\n");
		return NULL;
	}

	r_n = xmlDocGetRootElement(sqld);
	if( !r_n || (xmlStrcmp(r_n->name, (xmlChar *) "sqldata") != 0) ) {
		fprintf(stderr, "** ERROR ** Input XML document is not a valid sqldata document\n");
		return NULL;
	}

	r_n = xmlFindNode(r_n, "records");
	if( !r_n || !r_n->children ) {
		fprintf(stderr, "** ERROR ** Input XML document does not contain a records section\n");
		return NULL;
	}

	foreach_xmlnode(r_n->children, r_n) {
		if( (r_n->type != XML_ELEMENT_NODE)
		    || xmlStrcmp(r_n->name, (xmlChar *) "record") != 0 ) {
			// Skip uninteresting nodes
			continue;
		}
		if( rc == recid ) {
			xmlNode *v_n = NULL;
			// The rigth record is found, find the field we're looking for
			foreach_xmlnode(r_n->children, v_n) {
				char *fid_s = NULL;
				if( (v_n->type != XML_ELEMENT_NODE)
				    || (xmlStrcmp(v_n->name, (xmlChar *) "value") != 0) ) {
					// Skip uninteresting nodes
					continue;
				}
				fid_s = xmlGetAttrValue(v_n->properties, "fid");
				if( fid_s && (fid == atoi_nullsafe(fid_s)) ) {
					return sqldataExtractContent(v_n);
				}
			}
		}
		rc++;
	}
	return NULL;
}