summaryrefslogtreecommitdiffstats
path: root/sudoers/worker.c
diff options
context:
space:
mode:
authorSumit Bose <sbose@nb.localdomain>2008-10-11 00:00:52 +0200
committerSumit Bose <sbose@nb.localdomain>2008-10-11 00:00:52 +0200
commit1cde6f5091e98765e0f936b96e88b0e0c15ff4aa (patch)
treebf686f87999fe4f457926873876d0640a3d96732 /sudoers/worker.c
parent3fdccfc21786437f93623a6bb62d1a9e80a5c2b3 (diff)
downloadipa_policy-1cde6f5091e98765e0f936b96e88b0e0c15ff4aa.tar.gz
ipa_policy-1cde6f5091e98765e0f936b96e88b0e0c15ff4aa.tar.xz
ipa_policy-1cde6f5091e98765e0f936b96e88b0e0c15ff4aa.zip
added a small tool to explore libxml2 and libxslt
Diffstat (limited to 'sudoers/worker.c')
-rw-r--r--sudoers/worker.c106
1 files changed, 106 insertions, 0 deletions
diff --git a/sudoers/worker.c b/sudoers/worker.c
new file mode 100644
index 0000000..3d5d637
--- /dev/null
+++ b/sudoers/worker.c
@@ -0,0 +1,106 @@
+#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>
+
+
+int main(int argc, char **argv) {
+
+ int i;
+ xmlChar *str;
+ xmlDocPtr doc;
+ xmlXPathContextPtr xpathCtx;
+ xmlXPathObjectPtr xpathObj;
+/* 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 *xpathExpr_rng = (xmlChar *) "//su:RNGfile";
+ xmlChar *xpathExpr_xslt = (xmlChar *) "//su:XSLTfile";
+ xmlNodeSetPtr nodeset;
+
+ 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);
+ }
+
+ /* Create xpath evaluation context */
+ xpathCtx = xmlXPathNewContext(doc);
+ if(xpathCtx == NULL) {
+ fprintf(stderr,"Error: unable to create new XPath context\n");
+ xmlFreeDoc(doc);
+ exit(1);
+ }
+
+
+ /* Register a namespace */
+ if(xmlXPathRegisterNs(xpathCtx, "su", "http://freeipa.org/xml/rng/sudo/sudoers/1.0") != 0) {
+ fprintf(stderr,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", "", "http://freeipa.org/xml/rng/sudo/sudoers/1.0");
+ xmlXPathFreeContext(xpathCtx);
+ xmlFreeDoc(doc);
+ exit(1);
+ }
+
+
+ /* Evaluate xpath expression */
+ xpathObj = xmlXPathEvalExpression(xpathExpr_xslt, xpathCtx);
+ if(xpathObj == NULL) {
+ fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", xpathExpr_xslt);
+ xmlXPathFreeContext(xpathCtx);
+ xmlFreeDoc(doc);
+ return(-1);
+ }
+
+ if (xmlXPathNodeSetIsEmpty(xpathObj->nodesetval)) {
+ printf("Nothing found ...\n");
+ } else {
+ nodeset=xpathObj->nodesetval;
+ for(i=0; i<nodeset->nodeNr; i++) {
+ str = xmlNodeListGetString(doc, nodeset->nodeTab[i]->xmlChildrenNode, 1);
+ printf("--%s--\n", str);
+ xmlFree(str);
+ }
+ }
+ /* Evaluate xpath expression */
+ xpathObj = xmlXPathEvalExpression(xpathExpr_rng, xpathCtx);
+ if(xpathObj == NULL) {
+ fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", xpathExpr_rng);
+ xmlXPathFreeContext(xpathCtx);
+ xmlFreeDoc(doc);
+ return(-1);
+ }
+
+ if (xmlXPathNodeSetIsEmpty(xpathObj->nodesetval)) {
+ printf("Nothing found ...\n");
+ } else {
+ nodeset=xpathObj->nodesetval;
+ for(i=0; i<nodeset->nodeNr; i++) {
+ str = xmlNodeListGetString(doc, nodeset->nodeTab[i]->xmlChildrenNode, 1);
+ printf("--%s--\n", str);
+ xmlFree(str);
+ }
+ }
+
+ /* Cleanup */
+ xmlXPathFreeObject(xpathObj);
+ xmlXPathFreeContext(xpathCtx);
+ xmlFreeDoc(doc);
+
+
+
+
+ return(0);
+}