1 /*
2  * contrib/xml2/xslt_proc.c
3  *
4  * XSLT processing functions (requiring libxslt)
5  *
6  * John Gray, for Torchbox 2003-04-01
7  */
8 #include "postgres.h"
9 
10 #include "executor/spi.h"
11 #include "fmgr.h"
12 #include "funcapi.h"
13 #include "miscadmin.h"
14 #include "utils/builtins.h"
15 #include "utils/xml.h"
16 
17 #ifdef USE_LIBXSLT
18 
19 /* libxml includes */
20 
21 #include <libxml/xpath.h>
22 #include <libxml/tree.h>
23 #include <libxml/xmlmemory.h>
24 
25 /* libxslt includes */
26 
27 #include <libxslt/xslt.h>
28 #include <libxslt/xsltInternals.h>
29 #include <libxslt/security.h>
30 #include <libxslt/transform.h>
31 #include <libxslt/xsltutils.h>
32 #endif   /* USE_LIBXSLT */
33 
34 
35 #ifdef USE_LIBXSLT
36 
37 /* declarations to come from xpath.c */
38 extern PgXmlErrorContext *pgxml_parser_init(PgXmlStrictness strictness);
39 
40 /* local defs */
41 static const char **parse_params(text *paramstr);
42 #endif   /* USE_LIBXSLT */
43 
44 
45 PG_FUNCTION_INFO_V1(xslt_process);
46 
47 Datum
xslt_process(PG_FUNCTION_ARGS)48 xslt_process(PG_FUNCTION_ARGS)
49 {
50 #ifdef USE_LIBXSLT
51 
52 	text	   *doct = PG_GETARG_TEXT_P(0);
53 	text	   *ssheet = PG_GETARG_TEXT_P(1);
54 	text	   *result;
55 	text	   *paramstr;
56 	const char **params;
57 	PgXmlErrorContext *xmlerrcxt;
58 	volatile xsltStylesheetPtr stylesheet = NULL;
59 	volatile xmlDocPtr doctree = NULL;
60 	volatile xmlDocPtr restree = NULL;
61 	volatile xsltSecurityPrefsPtr xslt_sec_prefs = NULL;
62 	volatile xsltTransformContextPtr xslt_ctxt = NULL;
63 	volatile int resstat = -1;
64 	xmlChar    *resstr = NULL;
65 	int			reslen = 0;
66 
67 	if (fcinfo->nargs == 3)
68 	{
69 		paramstr = PG_GETARG_TEXT_P(2);
70 		params = parse_params(paramstr);
71 	}
72 	else
73 	{
74 		/* No parameters */
75 		params = (const char **) palloc(sizeof(char *));
76 		params[0] = NULL;
77 	}
78 
79 	/* Setup parser */
80 	xmlerrcxt = pgxml_parser_init(PG_XML_STRICTNESS_LEGACY);
81 
82 	PG_TRY();
83 	{
84 		xmlDocPtr	ssdoc;
85 		bool		xslt_sec_prefs_error;
86 
87 		/* Parse document */
88 		doctree = xmlParseMemory((char *) VARDATA(doct),
89 								 VARSIZE(doct) - VARHDRSZ);
90 
91 		if (doctree == NULL)
92 			xml_ereport(xmlerrcxt, ERROR, ERRCODE_EXTERNAL_ROUTINE_EXCEPTION,
93 						"error parsing XML document");
94 
95 		/* Same for stylesheet */
96 		ssdoc = xmlParseMemory((char *) VARDATA(ssheet),
97 							   VARSIZE(ssheet) - VARHDRSZ);
98 
99 		if (ssdoc == NULL)
100 			xml_ereport(xmlerrcxt, ERROR, ERRCODE_EXTERNAL_ROUTINE_EXCEPTION,
101 						"error parsing stylesheet as XML document");
102 
103 		/* After this call we need not free ssdoc separately */
104 		stylesheet = xsltParseStylesheetDoc(ssdoc);
105 
106 		if (stylesheet == NULL)
107 			xml_ereport(xmlerrcxt, ERROR, ERRCODE_EXTERNAL_ROUTINE_EXCEPTION,
108 						"failed to parse stylesheet");
109 
110 		xslt_ctxt = xsltNewTransformContext(stylesheet, doctree);
111 
112 		xslt_sec_prefs_error = false;
113 		if ((xslt_sec_prefs = xsltNewSecurityPrefs()) == NULL)
114 			xslt_sec_prefs_error = true;
115 
116 		if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_READ_FILE,
117 								 xsltSecurityForbid) != 0)
118 			xslt_sec_prefs_error = true;
119 		if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_WRITE_FILE,
120 								 xsltSecurityForbid) != 0)
121 			xslt_sec_prefs_error = true;
122 		if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_CREATE_DIRECTORY,
123 								 xsltSecurityForbid) != 0)
124 			xslt_sec_prefs_error = true;
125 		if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_READ_NETWORK,
126 								 xsltSecurityForbid) != 0)
127 			xslt_sec_prefs_error = true;
128 		if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_WRITE_NETWORK,
129 								 xsltSecurityForbid) != 0)
130 			xslt_sec_prefs_error = true;
131 		if (xsltSetCtxtSecurityPrefs(xslt_sec_prefs, xslt_ctxt) != 0)
132 			xslt_sec_prefs_error = true;
133 
134 		if (xslt_sec_prefs_error)
135 			ereport(ERROR,
136 					(errmsg("could not set libxslt security preferences")));
137 
138 		restree = xsltApplyStylesheetUser(stylesheet, doctree, params,
139 										  NULL, NULL, xslt_ctxt);
140 
141 		if (restree == NULL)
142 			xml_ereport(xmlerrcxt, ERROR, ERRCODE_EXTERNAL_ROUTINE_EXCEPTION,
143 						"failed to apply stylesheet");
144 
145 		resstat = xsltSaveResultToString(&resstr, &reslen, restree, stylesheet);
146 	}
147 	PG_CATCH();
148 	{
149 		if (restree != NULL)
150 			xmlFreeDoc(restree);
151 		if (xslt_ctxt != NULL)
152 			xsltFreeTransformContext(xslt_ctxt);
153 		if (xslt_sec_prefs != NULL)
154 			xsltFreeSecurityPrefs(xslt_sec_prefs);
155 		if (stylesheet != NULL)
156 			xsltFreeStylesheet(stylesheet);
157 		if (doctree != NULL)
158 			xmlFreeDoc(doctree);
159 		xsltCleanupGlobals();
160 
161 		pg_xml_done(xmlerrcxt, true);
162 
163 		PG_RE_THROW();
164 	}
165 	PG_END_TRY();
166 
167 	xmlFreeDoc(restree);
168 	xsltFreeTransformContext(xslt_ctxt);
169 	xsltFreeSecurityPrefs(xslt_sec_prefs);
170 	xsltFreeStylesheet(stylesheet);
171 	xmlFreeDoc(doctree);
172 	xsltCleanupGlobals();
173 
174 	pg_xml_done(xmlerrcxt, false);
175 
176 	/* XXX this is pretty dubious, really ought to throw error instead */
177 	if (resstat < 0)
178 		PG_RETURN_NULL();
179 
180 	result = cstring_to_text_with_len((char *) resstr, reslen);
181 
182 	if (resstr)
183 		xmlFree(resstr);
184 
185 	PG_RETURN_TEXT_P(result);
186 #else							/* !USE_LIBXSLT */
187 
188 	ereport(ERROR,
189 			(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
190 			 errmsg("xslt_process() is not available without libxslt")));
191 	PG_RETURN_NULL();
192 #endif   /* USE_LIBXSLT */
193 }
194 
195 #ifdef USE_LIBXSLT
196 
197 static const char **
parse_params(text * paramstr)198 parse_params(text *paramstr)
199 {
200 	char	   *pos;
201 	char	   *pstr;
202 	char	   *nvsep = "=";
203 	char	   *itsep = ",";
204 	const char **params;
205 	int			max_params;
206 	int			nparams;
207 
208 	pstr = text_to_cstring(paramstr);
209 
210 	max_params = 20;			/* must be even! */
211 	params = (const char **) palloc((max_params + 1) * sizeof(char *));
212 	nparams = 0;
213 
214 	pos = pstr;
215 
216 	while (*pos != '\0')
217 	{
218 		if (nparams >= max_params)
219 		{
220 			max_params *= 2;
221 			params = (const char **) repalloc(params,
222 										  (max_params + 1) * sizeof(char *));
223 		}
224 		params[nparams++] = pos;
225 		pos = strstr(pos, nvsep);
226 		if (pos != NULL)
227 		{
228 			*pos = '\0';
229 			pos++;
230 		}
231 		else
232 		{
233 			/* No equal sign, so ignore this "parameter" */
234 			nparams--;
235 			break;
236 		}
237 
238 		/* since max_params is even, we still have nparams < max_params */
239 		params[nparams++] = pos;
240 		pos = strstr(pos, itsep);
241 		if (pos != NULL)
242 		{
243 			*pos = '\0';
244 			pos++;
245 		}
246 		else
247 			break;
248 	}
249 
250 	/* Add the terminator marker; we left room for it in the palloc's */
251 	params[nparams] = NULL;
252 
253 	return params;
254 }
255 
256 #endif   /* USE_LIBXSLT */
257