summaryrefslogtreecommitdiffstats
path: root/sudoers/worker.c
blob: 492fa91e5129a26056c6578f92ecd413e996e027 (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
#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);
}