summaryrefslogtreecommitdiffstats
path: root/worker
diff options
context:
space:
mode:
authorSumit Bose <sbose@nb.localdomain>2008-10-14 13:48:04 +0200
committerSumit Bose <sbose@nb.localdomain>2008-10-14 13:48:04 +0200
commitbc009a849c81888f45f123f3bea473df8fd494f6 (patch)
treed90e6ff9d5a6c1430e94ca804d4163b0eab2c3fa /worker
parent2fe09ee550be101df2a12c29eecb9c0d12a1910c (diff)
downloadipa_policy-bc009a849c81888f45f123f3bea473df8fd494f6.tar.gz
ipa_policy-bc009a849c81888f45f123f3bea473df8fd494f6.tar.xz
ipa_policy-bc009a849c81888f45f123f3bea473df8fd494f6.zip
moved worker to new directory
Diffstat (limited to 'worker')
-rw-r--r--worker/Makefile5
-rw-r--r--worker/worker.c212
2 files changed, 217 insertions, 0 deletions
diff --git a/worker/Makefile b/worker/Makefile
new file mode 100644
index 0000000..1e7c6cf
--- /dev/null
+++ b/worker/Makefile
@@ -0,0 +1,5 @@
+
+CFLAGS=-Wall `xml2-config --cflags` `xslt-config --cflags`
+LDFLAGS=`xml2-config --libs` `xslt-config --libs`
+worker: worker.c
+ $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $+
diff --git a/worker/worker.c b/worker/worker.c
new file mode 100644
index 0000000..492fa91
--- /dev/null
+++ b/worker/worker.c
@@ -0,0 +1,212 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <assert.h>
+
+#include <libxml/tree.h>
+#include <libxml/parser.h>
+#include <libxml/xpath.h>
+#include <libxml/xpathInternals.h>
+#include <libxml/relaxng.h>
+
+#include <libxslt/xslt.h>
+#include <libxslt/xsltInternals.h>
+#include <libxslt/transform.h>
+#include <libxslt/xsltutils.h>
+
+#define XMLCHARLEN 255
+/* If a default namespace is defined
+ *
+ * IMPORTANT: XPath 1.0 has no concept of a default namespace. Unprefixed names in XPath only match names which have no namespace.
+ * So, if the document uses a default namespace, it is required to associate a non-empty prefix with the default namespace
+ * via register-namespace and add that prefix to names in XPath expressions intended to match nodes in the default namespace.
+ */
+xmlChar *default_namespace_prefix = (xmlChar *) "def";
+
+char * find_value_by_xpath(xmlDocPtr doc, xmlChar * xpathExpr, xmlChar *prefix, xmlChar * namespace)
+{
+
+ xmlXPathContextPtr xpathCtx;
+ xmlXPathObjectPtr xpathObj;
+ char *result=NULL;
+ xmlNodeSetPtr nodeset;
+ int i;
+ xmlChar *str;
+
+ /* Create xpath evaluation context */
+ xpathCtx = xmlXPathNewContext(doc);
+ if (xpathCtx == NULL) {
+ fprintf(stderr, "Error: unable to create new XPath context\n");
+ return(NULL);
+ }
+
+
+ /* Register a namespace */
+ if (xmlXPathRegisterNs (xpathCtx, prefix, namespace) != 0) {
+ fprintf(stderr,
+ "Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n",
+ "my", namespace);
+ xmlXPathFreeContext(xpathCtx);
+ return(NULL);
+ }
+ /* Evaluate xpath expression */
+ xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx);
+ if (xpathObj == NULL) {
+ fprintf(stderr,
+ "Error: unable to evaluate xpath expression \"%s\"\n", xpathExpr);
+ xmlXPathFreeContext(xpathCtx);
+ return(NULL);
+ }
+
+ if (xmlXPathNodeSetIsEmpty(xpathObj->nodesetval)) {
+ printf("Nothing found ...\n");
+ xmlXPathFreeObject(xpathObj);
+ xmlXPathFreeContext(xpathCtx);
+ return(NULL);
+ } else if (xmlXPathNodeSetGetLength(xpathObj->nodesetval) != 1 ) {
+ fprintf(stderr,"More than one node found!");
+ xmlXPathFreeObject(xpathObj);
+ xmlXPathFreeContext(xpathCtx);
+ return(NULL);
+ } else {
+ nodeset = xpathObj->nodesetval;
+/* FIXME: only allow one found value */
+ for (i = 0; i < nodeset->nodeNr; i++) {
+ str = xmlNodeListGetString(doc, nodeset->nodeTab[i]->xmlChildrenNode, 1);
+ result = strdup((char *) str);
+ xmlFree(str);
+ }
+ }
+
+
+ xmlXPathFreeObject(xpathObj);
+ xmlXPathFreeContext(xpathCtx);
+ return result;
+
+}
+
+int main(int argc, char **argv)
+{
+
+ xmlDocPtr doc;
+ xmlNodePtr rootNode;
+ xmlChar *default_namespace;
+ xmlChar xpathExpr[XMLCHARLEN];
+ char *rngFileName;
+ char *xsltFileName;
+ char *output_file_name;
+ char *output_file_owner;
+ char *output_file_group;
+ char *output_file_permission;
+ xmlRelaxNGValidCtxtPtr rngCtx;
+ xmlDocPtr xsltDoc;
+ xsltStylesheetPtr cur = NULL;
+ xmlDocPtr res;
+ int ret;
+
+ if (argc != 2) {
+ fprintf(stderr,
+ "missing or to many arguments, I expect a single filename!\n");
+ exit(1);
+ }
+
+ doc = xmlParseFile(argv[1]);
+ if (doc == NULL) {
+ fprintf(stderr, "Cannot parse document %s!\n", argv[1]);
+ exit(1);
+ }
+
+ /* find the default namespace */
+ rootNode=xmlDocGetRootElement(doc);
+ if(rootNode==NULL) {
+ fprintf(stderr, "Cannot find root node of document %s!\n", argv[1]);
+ exit(1);
+ }
+ if(xmlStrncasecmp(rootNode->name,(xmlChar *) "IPA", XMLCHARLEN) != 0) {
+ fprintf(stderr, "Name of root node of document %s has to be 'ipa'!\n", argv[1]);
+ exit(1);
+ }
+ if(rootNode->ns->href==NULL) {
+ fprintf(stderr, "Root node of document %s must define a namespace!\n", argv[1]);
+ exit(1);
+ }
+ default_namespace=xmlStrndup(rootNode->ns->href,XMLCHARLEN);
+ if(default_namespace==NULL) {
+ fprintf(stderr, "Cannot copy namespace!\n");
+ exit(1);
+ }
+
+ /* extract XSTLfile and RNGfile from document using XPath */
+ xmlStrPrintf(xpathExpr, XMLCHARLEN, (xmlChar *) "//%s:XSLTfile", default_namespace_prefix);
+ xsltFileName=find_value_by_xpath(doc,xpathExpr, default_namespace_prefix, default_namespace);
+ printf("--%s--\n", xsltFileName);
+ xmlStrPrintf(xpathExpr, XMLCHARLEN, (xmlChar *) "//%s:RNGfile", default_namespace_prefix);
+ rngFileName=find_value_by_xpath(doc,xpathExpr, default_namespace_prefix, default_namespace);
+ printf("--%s--\n", rngFileName);
+
+
+
+ /* validate the document */
+ rngCtx =
+ xmlRelaxNGNewValidCtxt(xmlRelaxNGParse
+ (xmlRelaxNGNewParserCtxt(rngFileName)));
+ if (rngCtx == NULL) {
+ fprintf(stderr, "Failed to create RNG context\n");
+ exit(-1);
+ }
+
+ if (xmlRelaxNGValidateDoc(rngCtx, doc) == 0) {
+ printf("The document is valid.\n");
+ } else {
+ fprintf(stderr, "Error during validation.\n");
+ }
+
+ xmlRelaxNGFreeValidCtxt(rngCtx);
+ free(rngFileName);
+
+
+ /* read the xslt file */
+ xsltDoc = xmlParseFile(xsltFileName);
+ if (xsltDoc == NULL) {
+ fprintf(stderr, "Cannot parse file %s!\n", xsltFileName);
+ exit(1);
+ }
+
+ output_file_name=find_value_by_xpath(xsltDoc,(xmlChar *) "//md:output_file/@name",(xmlChar *) "md", (xmlChar *) "http://freeipa.org/xsl/metadata/1.0");
+ output_file_owner=find_value_by_xpath(xsltDoc,(xmlChar *) "//md:output_file/@owner",(xmlChar *) "md", (xmlChar *) "http://freeipa.org/xsl/metadata/1.0");
+ output_file_group=find_value_by_xpath(xsltDoc,(xmlChar *) "//md:output_file/@group",(xmlChar *) "md", (xmlChar *) "http://freeipa.org/xsl/metadata/1.0");
+ output_file_permission=find_value_by_xpath(xsltDoc,(xmlChar *) "//md:output_file/@permission",(xmlChar *) "md", (xmlChar *) "http://freeipa.org/xsl/metadata/1.0");
+ printf("-%s-\n",output_file_name);
+ printf("-%s-\n",output_file_owner);
+ printf("-%s-\n",output_file_group);
+ printf("-%s-\n",output_file_permission);
+
+ cur = xsltParseStylesheetDoc(xsltDoc);
+ if (cur == NULL) {
+ fprintf(stderr, "Cannot parse stylesheet %s!\n", xsltFileName);
+ exit(1);
+ }
+
+ res = xsltApplyStylesheet(cur, doc, NULL);
+ if (xsltDoc == NULL) {
+ fprintf(stderr, "Cannot apply stylesheet %s!\n", xsltFileName);
+ exit(1);
+ }
+
+ ret = xsltSaveResultToFile(stdout, res, cur);
+ if (ret == -1) {
+ fprintf(stderr, "Cannot save result!\n");
+ exit(1);
+ }
+ xmlFreeDoc(res);
+
+ xsltFreeStylesheet(cur);
+ free(xsltFileName);
+
+ xmlFreeDoc(doc);
+
+
+
+
+ return (0);
+}