summaryrefslogtreecommitdiffstats
path: root/database
diff options
context:
space:
mode:
authorDavid Sommerseth <dazo@users.sourceforge.net>2009-09-22 00:22:10 +0200
committerDavid Sommerseth <dazo@users.sourceforge.net>2009-09-22 00:22:10 +0200
commit678de6fcad1bac80ea4a9554a46377ac2b927393 (patch)
tree951a60fc7a3307b3b2d036f18908f371b0866582 /database
parent5b0b63ab816ce0cfd01b851efee3ffdadd644eae (diff)
downloadeurephia-678de6fcad1bac80ea4a9554a46377ac2b927393.tar.gz
eurephia-678de6fcad1bac80ea4a9554a46377ac2b927393.tar.xz
eurephia-678de6fcad1bac80ea4a9554a46377ac2b927393.zip
Unified eDBadminGetCertificateInfo(), eDBadminAddCertificate() and eDBadminDeleteCertificate() into eDBadminCertificate()
Updated eurephiadm utility to make use of this new unified API
Diffstat (limited to 'database')
-rw-r--r--database/eurephiadb.c4
-rw-r--r--database/eurephiadb_driver.h9
-rw-r--r--database/sqlite/administration/certificates.c212
3 files changed, 126 insertions, 99 deletions
diff --git a/database/eurephiadb.c b/database/eurephiadb.c
index e01b4f1..6a12aea 100644
--- a/database/eurephiadb.c
+++ b/database/eurephiadb.c
@@ -121,9 +121,7 @@ int eDBlink_init(eurephiaCTX *ctx, const char *dbdriver, const int minver)
eDBadminUpdateUser = eGetSym(ctx, ctx->eurephia_driver, "eDBadminUpdateUser");
eDBadminDeleteUser = eGetSym(ctx, ctx->eurephia_driver, "eDBadminDeleteUser");
- eDBadminGetCertificateInfo = eGetSym(ctx, ctx->eurephia_driver, "eDBadminGetCertificateInfo");
- eDBadminAddCertificate = eGetSym(ctx, ctx->eurephia_driver, "eDBadminAddCertificate");
- eDBadminDeleteCertificate = eGetSym(ctx, ctx->eurephia_driver, "eDBadminDeleteCertificate");
+ eDBadminCertificate = eGetSym(ctx, ctx->eurephia_driver, "eDBadminCertificate");
eDBadminUserCertsLink = eGetSym(ctx, ctx->eurephia_driver, "eDBadminUserCertsLink");
diff --git a/database/eurephiadb_driver.h b/database/eurephiadb_driver.h
index 57b51e0..d90b2f5 100644
--- a/database/eurephiadb_driver.h
+++ b/database/eurephiadb_driver.h
@@ -604,7 +604,7 @@ int (*eDBadminDeleteUser) (eurephiaCTX *ctx, const int uid, xmlDoc *userinfo);
*
* @see eurephiaXML_CreateDoc(), eurephiaXML_getRoot()
*/
-xmlDoc *(*eDBadminGetCertificateInfo) (eurephiaCTX *ctx, xmlDoc *srchxml, const char *sortkeys);
+// xmlDoc *(*eDBadminGetCertificateInfo) (eurephiaCTX *ctx, xmlDoc *srchxml, const char *sortkeys);
/**
@@ -632,7 +632,7 @@ xmlDoc *(*eDBadminGetCertificateInfo) (eurephiaCTX *ctx, xmlDoc *srchxml, const
* @return The function returns certid of the newly registered certificate on success, otherwise -1
* @see eurephiaXML_CreateDoc()
*/
-int (*eDBadminAddCertificate) (eurephiaCTX *ctx, xmlDoc *certinfo_xml);
+// int (*eDBadminAddCertificate) (eurephiaCTX *ctx, xmlDoc *certinfo_xml);
/**
@@ -660,7 +660,10 @@ int (*eDBadminAddCertificate) (eurephiaCTX *ctx, xmlDoc *certinfo_xml);
* @return Returns 1 on success, otherwise 0.
* @see eurephiaXML_CreateDoc()
*/
-int (*eDBadminDeleteCertificate) (eurephiaCTX *ctx, xmlDoc *certinfo_xml);
+// int (*eDBadminDeleteCertificate) (eurephiaCTX *ctx, xmlDoc *certinfo_xml);
+
+
+xmlDoc *(*eDBadminCertificate) (eurephiaCTX *ctx, xmlDoc *certxml);
/**
diff --git a/database/sqlite/administration/certificates.c b/database/sqlite/administration/certificates.c
index f103827..53b50c2 100644
--- a/database/sqlite/administration/certificates.c
+++ b/database/sqlite/administration/certificates.c
@@ -58,44 +58,25 @@
void xmlReplaceChars(xmlChar *str, char s, char r);
/**
- * @copydoc eDBadminGetCertificateInfo()
+ * Internal function. Retrieves info about one or more certificates
+ *
+ * @param ctx eurephiaCTX
+ * @param srch_map eDBfieldMap defining the query
+ * @param sortkeys String containing the sort order of the fields
+ *
+ * @return Returns a valid xmlDoc containing the query,
+ * or a eurephia XML document with an error message.
*/
-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;
+static xmlDoc *certificate_list(eurephiaCTX *ctx, eDBfieldMap *srch_map, const char *sortkeys) {
+ xmlDoc *certlist = NULL;
+ xmlNode *cert_n = NULL, *tmp_n = NULL;
+ eDBfieldMap *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 );
+ DEBUG(ctx, 21, "Function call: certificates_list(ctx, fieldMap, '%s')", sortkeys);
+ assert( (ctx != NULL) && (srch_map != NULL) );
// Replace spaces with underscore in common name and
// in organisation fields, to comply with OpenVPN standards
@@ -107,11 +88,11 @@ xmlDoc *eDBadminGetCertificateInfo(eurephiaCTX *ctx, xmlDoc *srchxml, const char
res = sqlite_query_mapped(ctx, SQL_SELECT,
"SELECT depth, digest, common_name, organisation, email, registered, certid"
- " FROM openvpn_certificates", NULL, srch_map, dbsort);
+ " FROM openvpn_certificates", NULL, srch_map, sortkeys);
if( res == NULL ) {
- eDBfreeMapping(srch_map);
eurephia_log(ctx, LOG_ERROR, 0, "Could not query the certificate table");
- return NULL;
+ return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL,
+ "Could not query the database for certificate info");
}
memset(&tmp, 0, 2050);
@@ -138,45 +119,35 @@ xmlDoc *eDBadminGetCertificateInfo(eurephiaCTX *ctx, xmlDoc *srchxml, const char
sqlite_xml_value(tmp_n, XML_NODE, "email", res, i, 4);
}
sqlite_free_results(res);
- eDBfreeMapping(srch_map);
return certlist;
}
/**
- * @copydoc eDBadminAddCertificate()
+ * Internal function. Registers a new certificate and saves it in the database
+ *
+ * @param ctx eurephiaCTX
+ * @param crtinf_map eDBfieldMap containing certificate info to be registered
+ *
+ * @return Returns an eurephia ResultMsg XML document with a result string. On fatal errors,
+ * NULL is returned
*/
-int eDBadminAddCertificate(eurephiaCTX *ctx, xmlDoc *certinfo_xml) {
- xmlNode *crtinf_n = NULL;
- eDBfieldMap *crtinf_map = NULL, *ptr = NULL;
+static xmlDoc *certificate_add(eurephiaCTX *ctx, eDBfieldMap *crtinf_map) {
+ xmlDoc *res_d = NULL;
+ xmlNode *info_n = NULL;
+ eDBfieldMap *ptr = NULL;
dbresult *res = NULL;
- int certid = 0;
- DEBUG(ctx, 20, "Function call: eDBadminAddCertificate(ctx, xmlDoc)");
- assert( (ctx != NULL) && (certinfo_xml != NULL) );
+ DEBUG(ctx, 21, "Function call: certificate_add(ctx, xmlDoc)");
+ assert( (ctx != NULL) && (crtinf_map != 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;
+ return NULL;
}
- 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 ) {
@@ -186,53 +157,53 @@ int eDBadminAddCertificate(eurephiaCTX *ctx, xmlDoc *certinfo_xml) {
}
// Register the certificate
- res = sqlite_query_mapped(ctx, SQL_INSERT, "INSERT INTO openvpn_certificates", crtinf_map, NULL, NULL);
+ 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;
+ res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Could not register the certificate");
} else {
- certid = res->last_insert_id;
+ xmlChar *certid = malloc_nullsafe(ctx, 34);
+ assert( certid != NULL );
+
+ // Prepare an information tag/node with the certid value
+ xmlStrPrintf(certid, 32, (xmlChar *) "%ld", res->last_insert_id);
+ info_n = xmlNewNode(NULL, (xmlChar *)"certificate");
+ xmlNewProp(info_n, (xmlChar *) "certid", certid);
+
+ res_d = eurephiaXML_ResultMsg(ctx, exmlRESULT, info_n,
+ "Certificate registered (certid %ld)", res->last_insert_id);
+ xmlFreeNode(info_n);
}
sqlite_free_results(res);
- eDBfreeMapping(crtinf_map);
- return certid;
+ return res_d;
}
/**
- * @copydoc eDBadminDeleteCertificate()
+ * Internal function. Deletes one or more certificates from the database
+ *
+ * @param ctx eurephiaCTX
+ * @param crtinf_map eDBfieldMap with information about certificate(s) to delete
+ *
+ * @return Returns an eurephia ResultMsg XML document with a result string. On fatal errors,
+ * NULL is returned
*/
-int eDBadminDeleteCertificate(eurephiaCTX *ctx, xmlDoc *certinfo_xml) {
- int rc = 0;
- xmlNode *crtinf_n = NULL;
- eDBfieldMap *crtinf_map = NULL, *ptr = NULL;
+static xmlDoc *certificate_delete(eurephiaCTX *ctx, eDBfieldMap *crtinf_map) {
+ xmlDoc *res_d = NULL;
+ eDBfieldMap *ptr = NULL;
dbresult *res = NULL;
- DEBUG(ctx, 20, "Function call: eDBadminDeleteCertificate(ctx, xmlDoc)");
- assert( (ctx != NULL) && (certinfo_xml != NULL) );
+ DEBUG(ctx, 21, "Function call: certificate_delete(ctx, xmlDoc)");
+ assert( (ctx != NULL) && (crtinf_map != 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;
+ return NULL;
}
- 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 ) {
@@ -242,15 +213,70 @@ int eDBadminDeleteCertificate(eurephiaCTX *ctx, xmlDoc *certinfo_xml) {
}
// Register the certificate
- res = sqlite_query_mapped(ctx, SQL_DELETE, "DELETE FROM openvpn_certificates", NULL, crtinf_map, NULL);
+ 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;
+ res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Could not delete the certificate(s)");
} else {
- rc = 1;
+ res_d = eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL, "%i %s deleted",
+ sqlite_get_affected_rows(res),
+ (sqlite_get_affected_rows(res) != 1
+ ? "certificates":"certificate")
+ );
}
sqlite_free_results(res);
- eDBfreeMapping(crtinf_map);
- return rc;
+ return res_d;
+}
+
+/**
+ * @copydoc eDBadminCertificate()
+ */
+xmlDoc *eDBadminCertificate(eurephiaCTX *ctx, xmlDoc *qryxml) {
+ eDBfieldMap *fmap = NULL;
+ char *mode = NULL;
+ xmlDoc *resxml = NULL;
+ xmlNode *root_n = NULL, *fieldmap_n = NULL;
+
+ DEBUG(ctx, 20, "Function call: eDBadminCertificate(ctx, {xmlDoc})");
+ assert( (ctx != NULL) && (qryxml != 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;
+ }
+
+ root_n = eurephiaXML_getRoot(ctx, qryxml, "certificates", 1);
+ if( root_n == NULL ) {
+ eurephia_log(ctx, LOG_CRITICAL, 0, "Invalid XML input.");
+ return NULL;
+ }
+ mode = xmlGetAttrValue(root_n->properties, "mode");
+ if( mode == NULL ) {
+ eurephia_log(ctx, LOG_ERROR, 0, "Missing mode attribute");
+ return NULL;
+ }
+
+ fieldmap_n = xmlFindNode(root_n, "fieldMapping");
+ if( fieldmap_n == NULL ) {
+ eurephia_log(ctx, LOG_ERROR, 0, "Missing fieldMapping");
+ return NULL;
+ }
+ fmap = eDBxmlMapping(ctx, tbl_sqlite_certs, NULL, fieldmap_n);
+
+ if( strcmp(mode, "list") == 0 ) {
+ char *sortkeys = xmlGetNodeContent(root_n, "sortkeys");
+ resxml = certificate_list(ctx, fmap, eDBmkSortKeyString(tbl_sqlite_certs, sortkeys));
+ } else if( strcmp(mode, "register") == 0 ) {
+ resxml = certificate_add(ctx, fmap);
+ } else if( strcmp(mode, "delete") == 0 ) {
+ resxml = certificate_delete(ctx, fmap);
+ } else {
+ eurephia_log(ctx, LOG_ERROR, 0, "Certificates - Unknown mode: '%s'", mode);
+ resxml = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Unknown mode '%s'", mode);
+ }
+ eDBfreeMapping(fmap);
+ return resxml;
}