summaryrefslogtreecommitdiffstats
path: root/database/sqlite/administration/certificates.c
diff options
context:
space:
mode:
authorDavid Sommerseth <dazo@users.sourceforge.net>2009-09-13 22:28:52 +0200
committerDavid Sommerseth <dazo@users.sourceforge.net>2009-09-13 22:28:52 +0200
commit6c9f69863b10de14e7a1e2f6d2a448978299414a (patch)
tree24805ab42de33fafe05da0ed8dbc9b96bfaaba7c /database/sqlite/administration/certificates.c
parent89813b9e5bbd8c9d3fcea71a8436255208c2782d (diff)
downloadeurephia-6c9f69863b10de14e7a1e2f6d2a448978299414a.tar.gz
eurephia-6c9f69863b10de14e7a1e2f6d2a448978299414a.tar.xz
eurephia-6c9f69863b10de14e7a1e2f6d2a448978299414a.zip
Moved certificate functions into sqlite/administration/certificates.c
Diffstat (limited to 'database/sqlite/administration/certificates.c')
-rw-r--r--database/sqlite/administration/certificates.c256
1 files changed, 256 insertions, 0 deletions
diff --git a/database/sqlite/administration/certificates.c b/database/sqlite/administration/certificates.c
new file mode 100644
index 0000000..f103827
--- /dev/null
+++ b/database/sqlite/administration/certificates.c
@@ -0,0 +1,256 @@
+/* certificates.c -- Certificate management
+ *
+ * GPLv2 only - Copyright (C) 2008, 2009
+ * David Sommerseth <dazo@users.sourceforge.net>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; version 2
+ * of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ */
+
+/**
+ * @file sqlite/administration/certificates.c
+ * @author David Sommerseth <dazo@users.sourceforge.net>
+ * @date 2009-09-13
+ *
+ * @brief Certificate management functions
+ *
+ */
+
+#include <string.h>
+#include <unistd.h>
+#include <assert.h>
+
+#include <libxml/tree.h>
+
+#include <sqlite3.h>
+
+#include <eurephia_nullsafe.h>
+#include <eurephia_context.h>
+#include <eurephia_log.h>
+#include <eurephia_xml.h>
+#include <eurephia_values.h>
+#include <eurephiadb_session_struct.h>
+#include <eurephiadb_mapping.h>
+#include <passwd.h>
+
+#ifndef DRIVER_MODE
+#define DRIVER_MODE
+#endif
+#include <eurephiadb_driver.h>
+
+#include "../sqlite.h"
+
+#define FMAP_CERTS /**< fieldmapping.h: Include declaration of tbl_sqlite_certs */
+#include "../fieldmapping.h"
+
+void xmlReplaceChars(xmlChar *str, char s, char r);
+
+/**
+ * @copydoc eDBadminGetCertificateInfo()
+ */
+xmlDoc *eDBadminGetCertificateInfo(eurephiaCTX *ctx, xmlDoc *srchxml, const char *sortkeys) {
+ xmlDoc *certlist = NULL;
+ xmlNode *srch_n = NULL, *cert_n = NULL, *tmp_n = NULL;
+ eDBfieldMap *srch_map = NULL, *ptr = NULL;
+ dbresult *res = NULL;
+ xmlChar tmp[2050];
+ char *dbsort = NULL;
+ int i;
+
+ DEBUG(ctx, 20, "Function call: eDBadminGetCertificateInfo(ctx, xmlDoc, '%s')", sortkeys);
+ assert( (ctx != NULL) && (srchxml != NULL) );
+
+ if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) {
+ eurephia_log(ctx, LOG_CRITICAL, 0,
+ "eurephia admin function call attempted with wrong context type");
+ return NULL;
+ }
+
+ if( sortkeys != NULL ) {
+ dbsort = eDBmkSortKeyString(tbl_sqlite_certs, sortkeys);
+ }
+
+ srch_n = eurephiaXML_getRoot(ctx, srchxml, "certificate_info", 1);
+ if( srch_n == NULL ) {
+ eurephia_log(ctx, LOG_ERROR, 0, "Could not find a valid XML for looking up certificates");
+ return NULL;
+ }
+
+ srch_n = xmlFindNode(srch_n, "fieldMapping");
+ if( srch_n == NULL ) {
+ eurephia_log(ctx, LOG_ERROR, 0, "Could not find a valid XML for looking up certificates");
+ return NULL;
+ }
+
+ srch_map = eDBxmlMapping(ctx, tbl_sqlite_certs, NULL, srch_n);
+ assert( srch_map != NULL );
+
+ // Replace spaces with underscore in common name and
+ // in organisation fields, to comply with OpenVPN standards
+ for( ptr = srch_map; ptr != NULL; ptr = ptr->next ) {
+ if( ptr->field_id & (FIELD_CNAME | FIELD_ORG) ) {
+ xmlReplaceChars((xmlChar *) ptr->value, ' ', '_');
+ }
+ }
+
+ res = sqlite_query_mapped(ctx, SQL_SELECT,
+ "SELECT depth, digest, common_name, organisation, email, registered, certid"
+ " FROM openvpn_certificates", NULL, srch_map, dbsort);
+ if( res == NULL ) {
+ eDBfreeMapping(srch_map);
+ eurephia_log(ctx, LOG_ERROR, 0, "Could not query the certificate table");
+ return NULL;
+ }
+
+ memset(&tmp, 0, 2050);
+ eurephiaXML_CreateDoc(ctx, 1, "certificates", &certlist, &cert_n);
+ xmlStrPrintf(tmp, 64, (xmlChar *) "%i", sqlite_get_numtuples(res));
+ xmlNewProp(cert_n, (xmlChar *) "certificates", (xmlChar *) tmp);
+
+ for( i = 0; i < sqlite_get_numtuples(res); i++ ) {
+ tmp_n = xmlNewChild(cert_n, NULL, (xmlChar *) "certificate", NULL);
+
+ sqlite_xml_value(tmp_n, XML_ATTR, "certid", res, i, 6);
+ sqlite_xml_value(tmp_n, XML_ATTR, "depth", res, i, 0);
+ sqlite_xml_value(tmp_n, XML_ATTR, "registered", res, i, 5);
+ sqlite_xml_value(tmp_n, XML_NODE, "digest", res, i, 1);
+
+ xmlStrPrintf(tmp, 2048, (xmlChar *) "%.2048s", sqlite_get_value(res, i, 2));
+ xmlReplaceChars(tmp, '_', ' ');
+ xmlNewChild(tmp_n, NULL, (xmlChar *) "common_name", tmp);
+
+ xmlStrPrintf(tmp, 2048, (xmlChar *) "%.2048s", sqlite_get_value(res, i, 3));
+ xmlReplaceChars(tmp, '_', ' ');
+ xmlNewChild(tmp_n, NULL, (xmlChar *) "organisation", tmp);
+
+ sqlite_xml_value(tmp_n, XML_NODE, "email", res, i, 4);
+ }
+ sqlite_free_results(res);
+ eDBfreeMapping(srch_map);
+
+ return certlist;
+}
+
+
+/**
+ * @copydoc eDBadminAddCertificate()
+ */
+int eDBadminAddCertificate(eurephiaCTX *ctx, xmlDoc *certinfo_xml) {
+ xmlNode *crtinf_n = NULL;
+ eDBfieldMap *crtinf_map = NULL, *ptr = NULL;
+ dbresult *res = NULL;
+ int certid = 0;
+
+ DEBUG(ctx, 20, "Function call: eDBadminAddCertificate(ctx, xmlDoc)");
+ assert( (ctx != NULL) && (certinfo_xml != NULL) );
+
+ if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) {
+ eurephia_log(ctx, LOG_CRITICAL, 0,
+ "eurephia admin function call attempted with wrong context type");
+ return 0;
+ }
+
+ crtinf_n = eurephiaXML_getRoot(ctx, certinfo_xml, "register_certificate", 1);
+ if( crtinf_n == NULL ) {
+ eurephia_log(ctx, LOG_ERROR, 0, "Could not find a valid XML for registering certificate");
+ return 0;
+ }
+
+ crtinf_n = xmlFindNode(crtinf_n, "fieldMapping");
+ if( crtinf_n == NULL ) {
+ eurephia_log(ctx, LOG_ERROR, 0, "Could not find a valid XML for registering certificate");
+ return 0;
+ }
+
+ crtinf_map = eDBxmlMapping(ctx, tbl_sqlite_certs, NULL, crtinf_n);
+ assert( crtinf_map != NULL );
+
+ // Replace spaces with underscore in common name and
+ // in organisation fields, to comply with OpenVPN standards
+ for( ptr = crtinf_map; ptr != NULL; ptr = ptr->next ) {
+ if( ptr->field_id & (FIELD_CNAME | FIELD_ORG) ) {
+ xmlReplaceChars((xmlChar *) ptr->value, ' ', '_');
+ }
+ }
+
+ // Register the certificate
+ res = sqlite_query_mapped(ctx, SQL_INSERT, "INSERT INTO openvpn_certificates", crtinf_map, NULL, NULL);
+ if( res == NULL ) {
+ eurephia_log(ctx, LOG_FATAL, 0, "Could not register the certificate");
+ certid = -1;
+ } else {
+ certid = res->last_insert_id;
+ }
+ sqlite_free_results(res);
+ eDBfreeMapping(crtinf_map);
+
+ return certid;
+}
+
+
+/**
+ * @copydoc eDBadminDeleteCertificate()
+ */
+int eDBadminDeleteCertificate(eurephiaCTX *ctx, xmlDoc *certinfo_xml) {
+ int rc = 0;
+ xmlNode *crtinf_n = NULL;
+ eDBfieldMap *crtinf_map = NULL, *ptr = NULL;
+ dbresult *res = NULL;
+
+ DEBUG(ctx, 20, "Function call: eDBadminDeleteCertificate(ctx, xmlDoc)");
+ assert( (ctx != NULL) && (certinfo_xml != NULL) );
+
+ if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) {
+ eurephia_log(ctx, LOG_CRITICAL, 0,
+ "eurephia admin function call attempted with wrong context type");
+ return 0;
+ }
+
+ crtinf_n = eurephiaXML_getRoot(ctx, certinfo_xml, "delete_certificate", 1);
+ if( crtinf_n == NULL ) {
+ eurephia_log(ctx, LOG_ERROR, 0, "Could not find a valid XML for the delete certificate request");
+ return 0;
+ }
+
+ crtinf_n = xmlFindNode(crtinf_n, "fieldMapping");
+ if( crtinf_n == NULL ) {
+ eurephia_log(ctx, LOG_ERROR, 0, "Could not find a valid XML for the delete certificate request");
+ return 0;
+ }
+
+ crtinf_map = eDBxmlMapping(ctx, tbl_sqlite_certs, NULL, crtinf_n);
+ assert( crtinf_map != NULL );
+
+ // Replace spaces with underscore in common name and
+ // in organisation fields, to comply with OpenVPN standards
+ for( ptr = crtinf_map; ptr != NULL; ptr = ptr->next ) {
+ if( ptr->field_id & (FIELD_CNAME | FIELD_ORG) ) {
+ xmlReplaceChars((xmlChar *) ptr->value, ' ', '_');
+ }
+ }
+
+ // Register the certificate
+ res = sqlite_query_mapped(ctx, SQL_DELETE, "DELETE FROM openvpn_certificates", NULL, crtinf_map, NULL);
+ if( res == NULL ) {
+ eurephia_log(ctx, LOG_FATAL, 0, "Could not complete the delete certificate request");
+ rc = 0;
+ } else {
+ rc = 1;
+ }
+ sqlite_free_results(res);
+ eDBfreeMapping(crtinf_map);
+
+ return rc;
+}