diff options
-rw-r--r-- | sudoers/sudoers.rng | 2 | ||||
-rw-r--r-- | worker/Makefile | 14 | ||||
-rw-r--r-- | worker/debug.c | 21 | ||||
-rw-r--r-- | worker/util.h | 23 | ||||
-rw-r--r-- | worker/worker.c | 193 |
5 files changed, 141 insertions, 112 deletions
diff --git a/sudoers/sudoers.rng b/sudoers/sudoers.rng index fa268ba..fa93072 100644 --- a/sudoers/sudoers.rng +++ b/sudoers/sudoers.rng @@ -36,7 +36,7 @@ xmlns:pa="http://freeipa.org/xml/rng/ns/plugable_architecture/1.0"> <define name="rng_filename"><value>sudoers.rng</value></define> <define name="xslt_filename"><value>sudoers.xsl</value></define> - <define name="application_name"><value>s<a/>udo</value></define> + <define name="application_name"><value>sudo</value></define> <include href="policy_metadata.rng"/> <start ns="http://freeipa.org/xml/rng/sudo/1.0"> diff --git a/worker/Makefile b/worker/Makefile index 1e7c6cf..36fbde7 100644 --- a/worker/Makefile +++ b/worker/Makefile @@ -1,5 +1,13 @@ -CFLAGS=-Wall `xml2-config --cflags` `xslt-config --cflags` +CFLAGS=-Wall -Werror `xml2-config --cflags` `xslt-config --cflags` LDFLAGS=`xml2-config --libs` `xslt-config --libs` -worker: worker.c - $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $+ + +SRCS = worker.c debug.c +OBJS = worker.o debug.o + +all: worker + +$(OBJS): util.h + +worker: worker.o debug.o + $(CC) $(LDFLAGS) -o $@ $+ diff --git a/worker/debug.c b/worker/debug.c new file mode 100644 index 0000000..27a6a5c --- /dev/null +++ b/worker/debug.c @@ -0,0 +1,21 @@ +/* taken from Stephen's sssd tree */ +#define _GNU_SOURCE +#include <stdio.h> +#include <stdarg.h> +#include <stdlib.h> + +int debug_level = 3; + +void debug_fn(const char *format, ...) +{ + va_list ap; + char *s = NULL; + + va_start(ap, format); + vasprintf(&s, format, ap); + va_end(ap); + + /*write(state.fd, s, strlen(s));*/ + fprintf(stderr, s); + free(s); +} diff --git a/worker/util.h b/worker/util.h new file mode 100644 index 0000000..503702a --- /dev/null +++ b/worker/util.h @@ -0,0 +1,23 @@ +#ifndef __WORKER_UTIL_H__ +#define __WORKER_UTIL_H__ + +extern int debug_level; +void debug_fn(const char *format, ...); + +#define DEBUG(level, body) do { \ + if (level <= debug_level) { \ + debug_fn("DEBUG-%d (%s,%d): %s: ", level, __FILE__, __LINE__ , __FUNCTION__); \ + debug_fn body; \ + } \ +} while(0); + + +#define CHECK_NULL_FATAL(pointer, message) do { \ + if (pointer == NULL) { \ + DEBUG(0, message) \ + exit(1); \ + } \ +} while(0); + +#endif /* __WORKER_UTIL_H__ */ + diff --git a/worker/worker.c b/worker/worker.c index 23c3de3..d7b24b5 100644 --- a/worker/worker.c +++ b/worker/worker.c @@ -14,6 +14,8 @@ #include <libxslt/transform.h> #include <libxslt/xsltutils.h> +#include "util.h" + #define XMLCHARLEN 255 /* If a default namespace is defined * @@ -26,60 +28,57 @@ */ xmlChar *default_namespace_prefix = (xmlChar *) "def"; -char *find_value_by_xpath(xmlDocPtr doc, xmlChar * xpathExpr, +char *find_value_by_xpath(xmlDocPtr doc, xmlChar * xpath_expr, xmlChar * prefix, xmlChar * namespace) { - xmlXPathContextPtr xpathCtx; - xmlXPathObjectPtr xpathObj; + xmlXPathContextPtr xpath_context; + xmlXPathObjectPtr xpath_obj; char *result = NULL; /* Create xpath evaluation context */ - xpathCtx = xmlXPathNewContext(doc); - if (xpathCtx == NULL) { - fprintf(stderr, "Error: unable to create new XPath context\n"); - return (NULL); - } + xpath_context = xmlXPathNewContext(doc); + CHECK_NULL_FATAL(xpath_context, ("Error: unable to create new XPath context\n")) /* 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); + if (xmlXPathRegisterNs(xpath_context, prefix, namespace) != 0) { + DEBUG(0, + ("Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", + "my", namespace)); + xmlXPathFreeContext(xpath_context); 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); + xpath_obj = xmlXPathEvalExpression(xpath_expr, xpath_context); + if (xpath_obj == NULL) { + DEBUG(0, + ("Error: unable to evaluate xpath expression \"%s\"\n", + xpath_expr)); + xmlXPathFreeContext(xpath_context); return (NULL); } - if (xmlXPathNodeSetIsEmpty(xpathObj->nodesetval)) { - printf("Nothing found ...\n"); - xmlXPathFreeObject(xpathObj); - xmlXPathFreeContext(xpathCtx); + if (xmlXPathNodeSetIsEmpty(xpath_obj->nodesetval)) { + DEBUG(0, ("Nothing found for %s\n", xpath_expr)); + xmlXPathFreeObject(xpath_obj); + xmlXPathFreeContext(xpath_context); return (NULL); - } else if (xmlXPathNodeSetGetLength(xpathObj->nodesetval) != 1) { - fprintf(stderr, "More than one node found!"); - xmlXPathFreeObject(xpathObj); - xmlXPathFreeContext(xpathCtx); + } else if (xmlXPathNodeSetGetLength(xpath_obj->nodesetval) != 1) { + DEBUG(0, ("More than one node found for %s!", xpath_expr)); + xmlXPathFreeObject(xpath_obj); + xmlXPathFreeContext(xpath_context); return (NULL); } else { result = (char *) xmlNodeListGetString(doc, - xpathObj->nodesetval-> + xpath_obj->nodesetval-> nodeTab[0]->xmlChildrenNode, 1); } - xmlXPathFreeObject(xpathObj); - xmlXPathFreeContext(xpathCtx); + xmlXPathFreeObject(xpath_obj); + xmlXPathFreeContext(xpath_context); return result; } @@ -88,143 +87,121 @@ int main(int argc, char **argv) { xmlDocPtr doc; - xmlNodePtr rootNode; + xmlNodePtr root_node; xmlChar *default_namespace; - xmlChar xpathExpr[XMLCHARLEN]; - char *rngFileName; - char *xsltFileName; + xmlChar xpath_expr[XMLCHARLEN]; + char *rng_file_name; + char *xslt_file_name; char *output_file_name; char *output_file_owner; char *output_file_group; char *output_file_permission; - xmlRelaxNGValidCtxtPtr rngCtx; - xmlDocPtr xsltDoc; + xmlRelaxNGValidCtxtPtr rng_context; + xmlDocPtr xslt_doc; xsltStylesheetPtr cur = NULL; xmlDocPtr res; int ret; if (argc != 2) { - fprintf(stderr, - "missing or to many arguments, I expect a single filename!\n"); + DEBUG(0, + ("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); - } + CHECK_NULL_FATAL(doc, ("Cannot parse document %s!\n", argv[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"); + root_node = xmlDocGetRootElement(doc); + CHECK_NULL_FATAL(root_node, ("Cannot find root node of document %s!\n", argv[1])) + + if (xmlStrncasecmp(root_node->name, (xmlChar *) "IPA", XMLCHARLEN) != 0) { + DEBUG(0, + ("Name of root node of document %s has to be 'ipa'!\n", + argv[1])); exit(1); } + CHECK_NULL_FATAL(root_node->ns->href, ("Root node of document %s must define a namespace!\n", argv[1])) + + default_namespace = xmlStrndup(root_node->ns->href, XMLCHARLEN); + CHECK_NULL_FATAL(default_namespace, ("Cannot copy namespace!\n")) /* extract XSTLfile and RNGfile from document using XPath */ - xmlStrPrintf(xpathExpr, XMLCHARLEN, (xmlChar *) "//%s:XSLTfile", + xmlStrPrintf(xpath_expr, XMLCHARLEN, (xmlChar *) "//%s:XSLTfile", default_namespace_prefix); - xsltFileName = - find_value_by_xpath(doc, xpathExpr, default_namespace_prefix, + xslt_file_name = + find_value_by_xpath(doc, xpath_expr, default_namespace_prefix, default_namespace); - printf("--%s--\n", xsltFileName); - xmlStrPrintf(xpathExpr, XMLCHARLEN, (xmlChar *) "//%s:RNGfile", + CHECK_NULL_FATAL(rng_file_name, ("Name of XSLT file not found.\n")) + DEBUG(3, ("Found name of XSLT file: %s\n", xslt_file_name)); + + xmlStrPrintf(xpath_expr, XMLCHARLEN, (xmlChar *) "//%s:RNGfile", default_namespace_prefix); - rngFileName = - find_value_by_xpath(doc, xpathExpr, default_namespace_prefix, + rng_file_name = + find_value_by_xpath(doc, xpath_expr, default_namespace_prefix, default_namespace); - printf("--%s--\n", rngFileName); + CHECK_NULL_FATAL(rng_file_name, ("Name of RELANX NG schema file not found.\n")) + DEBUG(3, ("Found name of RELAX NG schema file: %s\n", rng_file_name)); /* validate the document */ - rngCtx = + rng_context = xmlRelaxNGNewValidCtxt(xmlRelaxNGParse - (xmlRelaxNGNewParserCtxt(rngFileName))); - if (rngCtx == NULL) { - fprintf(stderr, "Failed to create RNG context\n"); - exit(-1); - } + (xmlRelaxNGNewParserCtxt(rng_file_name))); + CHECK_NULL_FATAL(rng_context, ("Failed to create RNG context\n")) - if (xmlRelaxNGValidateDoc(rngCtx, doc) == 0) { - printf("The document is valid.\n"); + if (xmlRelaxNGValidateDoc(rng_context, doc) == 0) { + DEBUG(0, ("The document is valid.\n")); } else { - fprintf(stderr, "Error during validation.\n"); + DEBUG(0, ("Error during validation.\n")); } - xmlRelaxNGFreeValidCtxt(rngCtx); - free(rngFileName); + xmlRelaxNGFreeValidCtxt(rng_context); + free(rng_file_name); /* read the xslt file */ - xsltDoc = xmlParseFile(xsltFileName); - if (xsltDoc == NULL) { - fprintf(stderr, "Cannot parse file %s!\n", xsltFileName); - exit(1); - } + xslt_doc = xmlParseFile(xslt_file_name); + CHECK_NULL_FATAL(xslt_doc, ("Cannot parse file %s!\n", xslt_file_name)) output_file_name = - find_value_by_xpath(xsltDoc, (xmlChar *) "//md:output_handler/file/@name", + find_value_by_xpath(xslt_doc, (xmlChar *) "//md:output_handler/md:file/@md:name", (xmlChar *) "md", (xmlChar *) "http://freeipa.org/xsl/metadata/1.0"); output_file_owner = - find_value_by_xpath(xsltDoc, (xmlChar *) "//md:output_handler/file/@owner", + find_value_by_xpath(xslt_doc, (xmlChar *) "//md:output_handler/md:file/@md:owner", (xmlChar *) "md", (xmlChar *) "http://freeipa.org/xsl/metadata/1.0"); output_file_group = - find_value_by_xpath(xsltDoc, (xmlChar *) "//md:output_handler/file/@group", + find_value_by_xpath(xslt_doc, (xmlChar *) "//md:output_handler/md:file/@md:group", (xmlChar *) "md", (xmlChar *) "http://freeipa.org/xsl/metadata/1.0"); output_file_permission = - find_value_by_xpath(xsltDoc, - (xmlChar *) "//md:output_handler/file/@permission", + find_value_by_xpath(xslt_doc, + (xmlChar *) "//md:output_handler/md:file/@md: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); - } + DEBUG(0, ("-%s-\n", output_file_name)); + DEBUG(0, ("-%s-\n", output_file_owner)); + DEBUG(0, ("-%s-\n", output_file_group)); + DEBUG(0, ("-%s-\n", output_file_permission)); + + cur = xsltParseStylesheetDoc(xslt_doc); + CHECK_NULL_FATAL(cur, ("Cannot parse stylesheet %s!\n", xslt_file_name)) res = xsltApplyStylesheet(cur, doc, NULL); - if (xsltDoc == NULL) { - fprintf(stderr, "Cannot apply stylesheet %s!\n", xsltFileName); - exit(1); - } + CHECK_NULL_FATAL(xslt_doc, ("Cannot apply stylesheet %s!\n", xslt_file_name)) ret = xsltSaveResultToFile(stdout, res, cur); if (ret == -1) { - fprintf(stderr, "Cannot save result!\n"); + DEBUG(0, ("Cannot save result!\n")); exit(1); } xmlFreeDoc(res); xsltFreeStylesheet(cur); - free(xsltFileName); + free(xslt_file_name); xmlFreeDoc(doc); |