Debian Codegraph @

database/sqlite/administration/certificates.c

raw
1 /* certificates.c -- Certificate management
2 *
3 * GPLv2 only - Copyright (C) 2008 - 2012
4 * David Sommerseth <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; version 2
9 * of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *
20 */
21
22 /**
23 * @file sqlite/administration/certificates.c
24 * @author David Sommerseth <[email protected]>
25 * @date 2009-09-13
26 *
27 * @brief Certificate management functions
28 *
29 */
30
31 #include <string.h>
32 #include <unistd.h>
33 #include <assert.h>
34
35 #include <libxml/tree.h>
36
37 #include <sqlite3.h>
38
39 #include <eurephia_nullsafe.h>
40 #include <eurephia_context.h>
41 #include <eurephia_log.h>
42 #include <eurephia_xml.h>
43 #include <eurephia_values.h>
44 #include <eurephiadb_session_struct.h>
45 #include <eurephiadb_mapping.h>
46 #include <passwd.h>
47
48 #ifndef DRIVER_MODE
49 #define DRIVER_MODE
50 #endif
51 #include <eurephiadb_driver.h>
52
53 #include "../sqlite.h"
54
55 #define FMAP_CERTS /**< fieldmapping.h: Include declaration of tbl_sqlite_certs */
56 #include "../fieldmapping.h"
57
58
59 /**
60 * Internal function. Retrieves info about one or more certificates
61 *
62 * @param ctx eurephiaCTX
63 * @param srch_map eDBfieldMap defining the query
64 * @param sortkeys String containing the sort order of the fields
65 *
66 * @return Returns a valid xmlDoc containing the query,
67 * or a eurephia XML document with an error message.
68 */
69 static xmlDoc *certificate_list(eurephiaCTX *ctx, eDBfieldMap *srch_map, const char *sortkeys) {
70 xmlDoc *certlist = NULL;
71 xmlNode *cert_n = NULL, *tmp_n = NULL;
72 eDBfieldMap *ptr = NULL;
73 dbresult *res = NULL;
74 xmlChar tmp[2050];
75 int i;
76
77 DEBUG(ctx, 21, "Function call: certificates_list(ctx, fieldMap, '%s')", sortkeys);
78 assert( (ctx != NULL) && (srch_map != NULL) );
79
80 // Replace spaces with underscore in common name and
81 // in organisation fields, to comply with OpenVPN standards
82 for( ptr = srch_map; ptr != NULL; ptr = ptr->next ) {
83 if( ptr->field_id & (FIELD_CNAME | FIELD_ORG) ) {
84 xmlReplaceChars((xmlChar *) ptr->value, ' ', '_');
85 }
86 }
87
88 res = sqlite_query_mapped(ctx, SQL_SELECT,
89 "SELECT depth, lower(digest), common_name, organisation, email, "
90 " locdt(registered), certid"
91 " FROM openvpn_certificates", NULL, srch_map, sortkeys);
92 if( sqlite_query_status(res) != dbSUCCESS ) {
93 eurephia_log(ctx, LOG_ERROR, 0, "Could not query the certificate table");
94 tmp_n = sqlite_log_error_xml(ctx, res);
95 certlist = eurephiaXML_ResultMsg(ctx, exmlERROR, tmp_n,
96 "Could not query the database for certificate info");
97 xmlFreeNode(tmp_n);
98 goto exit;
99 }
100
101 memset(&tmp, 0, 2050);
102 eurephiaXML_CreateDoc(ctx, 1, "certificates", &certlist, &cert_n);
103 xmlStrPrintf(tmp, 64, "%ld", sqlite_get_numtuples(res));
104 xmlNewProp(cert_n, (xmlChar *) "certificates", (xmlChar *) tmp);
105
106 for( i = 0; i < sqlite_get_numtuples(res); i++ ) {
107 tmp_n = xmlNewChild(cert_n, NULL, (xmlChar *) "certificate", NULL);
108
109 sqlite_xml_value(tmp_n, XML_ATTR, "certid", res, i, 6);
110 sqlite_xml_value(tmp_n, XML_ATTR, "depth", res, i, 0);
111 sqlite_xml_value(tmp_n, XML_ATTR, "registered", res, i, 5);
112 sqlite_xml_value(tmp_n, XML_NODE, "digest", res, i, 1);
113
114 xmlStrPrintf(tmp, 2048, "%.2048s", sqlite_get_value(res, i, 2));
115 xmlReplaceChars(tmp, '_', ' ');
116 xmlNewChild(tmp_n, NULL, (xmlChar *) "common_name", tmp);
117
118 xmlStrPrintf(tmp, 2048, "%.2048s", sqlite_get_value(res, i, 3));
119 xmlReplaceChars(tmp, '_', ' ');
120 xmlNewChild(tmp_n, NULL, (xmlChar *) "organisation", tmp);
121
122 sqlite_xml_value(tmp_n, XML_NODE, "email", res, i, 4);
123 }
124
125 exit:
126 sqlite_free_results(res);
127 return certlist;
128 }
129
130
131 /**
132 * Internal function. Registers a new certificate and saves it in the database
133 *
134 * @param ctx eurephiaCTX
135 * @param crtinf_map eDBfieldMap containing certificate info to be registered
136 *
137 * @return Returns an eurephia ResultMsg XML document with a result string. On fatal errors,
138 * NULL is returned
139 */
140 static xmlDoc *certificate_add(eurephiaCTX *ctx, eDBfieldMap *crtinf_map) {
141 xmlDoc *res_d = NULL;
142 xmlNode *info_n = NULL;
143 eDBfieldMap *ptr = NULL;
144 dbresult *res = NULL;
145
146 DEBUG(ctx, 21, "Function call: certificate_add(ctx, xmlDoc)");
147 assert( (ctx != NULL) && (crtinf_map != NULL) );
148
149 if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) {
150 eurephia_log(ctx, LOG_CRITICAL, 0,
151 "eurephia admin function call attempted with wrong context type");
152 return NULL;
153 }
154
155 // Replace spaces with underscore in common name and
156 // in organisation fields, to comply with OpenVPN standards
157 for( ptr = crtinf_map; ptr != NULL; ptr = ptr->next ) {
158 if( ptr->field_id & (FIELD_CNAME | FIELD_ORG) ) {
159 xmlReplaceChars((xmlChar *) ptr->value, ' ', '_');
160 }
161 }
162
163 // Register the certificate
164 res = sqlite_query_mapped(ctx, SQL_INSERT, "INSERT INTO openvpn_certificates",
165 crtinf_map, NULL, NULL);
166 if( sqlite_query_status(res) != dbSUCCESS ) {
167 eurephia_log(ctx, LOG_FATAL, 0, "Could not register the certificate");
168 info_n = sqlite_log_error_xml(ctx, res);
169 res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, info_n, "Could not register the certificate");
170 xmlFreeNode(info_n);
171 } else {
172 xmlChar *certid = malloc_nullsafe(ctx, 34);
173 assert( certid != NULL );
174
175 // Prepare an information tag/node with the certid value
176 xmlStrPrintf(certid, 32, "%lld", res->last_insert_id);
177 info_n = xmlNewNode(NULL, (xmlChar *)"certificate");
178 xmlNewProp(info_n, (xmlChar *) "certid", certid);
179
180 res_d = eurephiaXML_ResultMsg(ctx, exmlRESULT, info_n,
181 "Certificate registered (certid %ld)", res->last_insert_id);
182 free_nullsafe(ctx, certid);
183 xmlFreeNode(info_n);
184 }
185 sqlite_free_results(res);
186
187 return res_d;
188 }
189
190
191 /**
192 * Internal function. Deletes one or more certificates from the database
193 *
194 * @param ctx eurephiaCTX
195 * @param crtinf_map eDBfieldMap with information about certificate(s) to delete
196 *
197 * @return Returns an eurephia ResultMsg XML document with a result string. On fatal errors,
198 * NULL is returned
199 */
200 static xmlDoc *certificate_delete(eurephiaCTX *ctx, eDBfieldMap *crtinf_map) {
201 xmlDoc *res_d = NULL;
202 eDBfieldMap *ptr = NULL;
203 dbresult *res = NULL;
204
205 DEBUG(ctx, 21, "Function call: certificate_delete(ctx, xmlDoc)");
206 assert( (ctx != NULL) && (crtinf_map != NULL) );
207
208 if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) {
209 eurephia_log(ctx, LOG_CRITICAL, 0,
210 "eurephia admin function call attempted with wrong context type");
211 return NULL;
212 }
213
214 // Replace spaces with underscore in common name and
215 // in organisation fields, to comply with OpenVPN standards
216 for( ptr = crtinf_map; ptr != NULL; ptr = ptr->next ) {
217 if( ptr->field_id & (FIELD_CNAME | FIELD_ORG) ) {
218 xmlReplaceChars((xmlChar *) ptr->value, ' ', '_');
219 }
220 }
221
222 // Register the certificate
223 res = sqlite_query_mapped(ctx, SQL_DELETE, "DELETE FROM openvpn_certificates",
224 NULL, crtinf_map, NULL);
225 if( sqlite_query_status(res) != dbSUCCESS ) {
226 xmlNode *err_n = NULL;
227
228 eurephia_log(ctx, LOG_FATAL, 0, "Could not complete the delete certificate request");
229 err_n = sqlite_log_error_xml(ctx, res);
230 res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, err_n, "Could not delete the certificate(s)");
231 xmlFreeNode(err_n);
232 } else {
233 res_d = eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL, "%i %s deleted",
234 sqlite_get_affected_rows(res),
235 (sqlite_get_affected_rows(res) != 1
236 ? "certificates":"certificate")
237 );
238 }
239 sqlite_free_results(res);
240
241 return res_d;
242 }
243
244 /**
245 * @copydoc eDBadminCertificate()
246 */
247 xmlDoc *eDBadminCertificate(eurephiaCTX *ctx, xmlDoc *qryxml) {
248 eDBfieldMap *fmap = NULL;
249 char *mode = NULL;
250 xmlDoc *resxml = NULL;
251 xmlNode *root_n = NULL, *fieldmap_n = NULL;
252
253 DEBUG(ctx, 20, "Function call: eDBadminCertificate(ctx, {xmlDoc})");
254 assert( (ctx != NULL) && (qryxml != NULL) );
255
256 if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) {
257 eurephia_log(ctx, LOG_CRITICAL, 0,
258 "eurephia admin function call attempted with wrong context type");
259 return NULL;
260 }
261
262 root_n = eurephiaXML_getRoot(ctx, qryxml, "certificates", 1);
263 if( root_n == NULL ) {
264 eurephia_log(ctx, LOG_CRITICAL, 0, "Invalid XML input.");
265 return NULL;
266 }
267 mode = xmlGetAttrValue(root_n->properties, "mode");
268 if( mode == NULL ) {
269 eurephia_log(ctx, LOG_ERROR, 0, "Missing mode attribute");
270 return NULL;
271 }
272
273 fieldmap_n = xmlFindNode(root_n, "fieldMapping");
274 if( fieldmap_n == NULL ) {
275 eurephia_log(ctx, LOG_ERROR, 0, "Missing fieldMapping");
276 return NULL;
277 }
278 fmap = eDBxmlMapping(ctx, tbl_sqlite_certs, NULL, fieldmap_n);
279
280 if( strcmp(mode, "list") == 0 ) {
281 char *sortkeys = xmlGetNodeContent(root_n, "sortkeys");
282 resxml = certificate_list(ctx, fmap, eDBmkSortKeyString(fmap, sortkeys));
283 } else if( strcmp(mode, "register") == 0 ) {
284 resxml = certificate_add(ctx, fmap);
285 } else if( strcmp(mode, "delete") == 0 ) {
286 resxml = certificate_delete(ctx, fmap);
287 } else {
288 eurephia_log(ctx, LOG_ERROR, 0, "Certificates - Unknown mode: '%s'", mode);
289 resxml = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Unknown mode '%s'", mode);
290 }
291 eDBfreeMapping(fmap);
292 return resxml;
293 }