Better element name validation. Namespaces in element names are broken
[pwmd.git] / src / xml.c
bloba411a12e7cfbc217dfe406f66d54edc98eb71712
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2006 Ben Kibbey <bjk@luxsci.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <err.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <ctype.h>
28 #include <glib.h>
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
34 #include "pwmd_error.h"
35 #include "xml.h"
37 gboolean valid_xml_element(const xmlChar *element)
39 const xmlChar *p;
41 if (!element || !*element || isdigit(*element))
42 return FALSE;
44 for (p = element; *p; p++) {
45 if (!isalnum(*p))
46 return FALSE;
49 if (xmlStrncasecmp(element, (xmlChar *)"?xml", 4) == 0)
50 return FALSE;
52 return TRUE;
55 gboolean new_account(xmlDocPtr doc, gchar *name)
57 xmlNodePtr root = xmlDocGetRootElement(doc);
58 xmlAttrPtr a;
59 xmlNodePtr n;
61 if (!name || !root)
62 return FALSE;
64 n = xmlNewNode(NULL, (xmlChar *)"account");
65 n = xmlAddChild(root, n);
66 a = xmlNewProp(n, (xmlChar *)"name", (xmlChar *)name);
67 return TRUE;
70 gchar *new_document()
72 const char *line =
73 "<?xml version=\"1.0\"?>\n"
74 "<!DOCTYPE accounts [\n"
75 "<!ELEMENT accounts (account*)>\n"
76 "<!ELEMENT account (username?, password?, smtp?, pop?, imap?)>\n"
77 "<!ATTLIST account name CDATA #REQUIRED>\n"
78 "<!ELEMENT username (#PCDATA)>\n"
79 "<!ELEMENT password (#PCDATA)>\n"
80 "<!ELEMENT smtp (hostname, port, ssl, sslfingerprint)>\n"
81 "<!ELEMENT pop (hostname, port, ssl, sslfingerprint)>\n"
82 "<!ELEMENT imap (hostname, port, ssl, sslfingerprint)>\n"
83 "<!ELEMENT hostname (#PCDATA)>\n"
84 "<!ELEMENT port (#PCDATA)>\n"
85 "<!ELEMENT ssl (#PCDATA)>\n"
86 "<!ELEMENT sslfingerprint (#PCDATA)>\n"
87 "]>\n"
88 "<accounts/>\n";
89 return g_strdup(line);
92 gboolean list_accounts(xmlTextReaderPtr reader, gchar **dst, gint *pwmd_errno)
94 xmlNodePtr n;
95 gchar *line = NULL;
96 gchar **str = NULL;
97 gint i = 0;
100 * "fast forward" to the first account.
102 while (xmlTextReaderRead(reader) == 1) {
103 if ((n = xmlTextReaderCurrentNode(reader)) == NULL) {
104 *pwmd_errno = EPWMD_LIBXML_ERROR;
105 return FALSE;
108 if (xmlTextReaderDepth(reader) == 1 &&
109 xmlStrcmp(n->name, (xmlChar *)"account") == 0 &&
110 xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) {
111 xmlChar *p = xmlTextReaderGetAttribute(reader, (xmlChar *)"name");
113 if (!p)
114 continue;
116 str = g_realloc(str, (i + 2) * sizeof(gchar *));
117 str[i++] = g_strdup((gchar *)p);
118 str[i] = NULL;
119 xmlFree(p);
123 if (!i || !str) {
124 *pwmd_errno = EPWMD_EMPTY_ELEMENT;
125 return FALSE;
128 line = g_strjoinv("\n", str);
129 g_strfreev(str);
130 *dst = line;
131 return TRUE;
134 gboolean find_account(xmlTextReaderPtr reader, gchar *name)
136 xmlNodePtr n;
138 while (xmlTextReaderRead(reader) == 1) {
139 if ((n = xmlTextReaderCurrentNode(reader)) == NULL)
140 return FALSE;
142 if (xmlTextReaderDepth(reader) == 1 &&
143 xmlStrcmp(n->name, (xmlChar *)"account") == 0 &&
144 xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) {
145 xmlChar *p = xmlTextReaderGetAttribute(reader, (xmlChar *)"name");
147 // Shouldn't happen (DTD).
148 if (!p)
149 continue;
151 if (xmlStrcmp(p, (xmlChar *)name) == 0) {
152 xmlFree(p);
153 return TRUE;
156 xmlFree(p);
160 return FALSE;
163 gint find_element(xmlTextReaderPtr reader, gchar *e, gint more)
165 gint type;
166 xmlNodePtr n;
168 while (xmlTextReaderRead(reader) == 1) {
169 if ((n = xmlTextReaderCurrentNode(reader)) == NULL)
170 return 1;
173 * Stop processing after the closing account element to prevent
174 * finding element names that may exist in another account.
176 if (xmlTextReaderDepth(reader) == 1 &&
177 xmlStrcmp(n->name, (xmlChar *)"account") == 0 &&
178 xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT)
179 return 1;
181 if (xmlStrcmp(n->name, (xmlChar *)e) == 0 &&
182 xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) {
183 if (more)
184 return 0;
186 if (xmlTextReaderNext(reader) != 1)
187 return 1;
189 type = xmlTextReaderNodeType(reader);
191 if (type != XML_READER_TYPE_TEXT)
192 return 2;
194 return 0;
198 return 1;
201 xmlNodePtr find_node(xmlNodePtr root, xmlChar *name)
203 xmlNodePtr n;
205 for (n = root->children; n; n = n->next) {
206 if (g_ascii_strcasecmp((gchar *)name, (gchar *)n->name) == 0)
207 return n;
210 return NULL;
213 gint open_xml(const gchar *data, gsize size, xmlDocPtr *doc,
214 xmlTextReaderPtr *reader)
216 if ((*doc = xmlReadMemory(data, size, NULL, "UTF-8",
217 XML_PARSE_NOBLANKS)) == NULL)
218 return 1;
220 if ((*reader = xmlReaderWalker(*doc)) == NULL)
221 return 2;
223 return 0;