diff options
| author | David Sommerseth <dazo@users.sourceforge.net> | 2009-08-29 09:54:06 +0200 |
|---|---|---|
| committer | David Sommerseth <dazo@users.sourceforge.net> | 2009-08-29 09:54:06 +0200 |
| commit | 2fcbada9a862ee19a77ee0505452ebed1f3926b1 (patch) | |
| tree | 88550102739b169fd7b5f1f7198dc1556faad8be /database/sqlite/usercerts.c | |
| parent | c22206b7038390e0fe6b14f1ecb1d7012b0ed86d (diff) | |
Moved usercerts related functions into its own file
Diffstat (limited to 'database/sqlite/usercerts.c')
| -rw-r--r-- | database/sqlite/usercerts.c | 182 |
1 files changed, 182 insertions, 0 deletions
diff --git a/database/sqlite/usercerts.c b/database/sqlite/usercerts.c new file mode 100644 index 0000000..31b72af --- /dev/null +++ b/database/sqlite/usercerts.c @@ -0,0 +1,182 @@ +/* usercerts.c -- Admin functions - user-certitificate table + * + * 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. + * + */ + +#include <string.h> +#include <unistd.h> +#include <assert.h> + +#include <libxml/tree.h> + +#ifndef DRIVERAPIVERSION +# define DRIVERAPIVERSION 2 +#endif + +#include <sqlite3.h> + +#include <eurephia_nullsafe.h> +#include <eurephia_context.h> +#include <eurephia_admin_struct.h> +#include <eurephia_log.h> +#include <eurephia_xml.h> +#include <eurephia_values.h> +#include <eurephiadb_session_struct.h> +#include <eurephiadb_mapping.h> + +#ifndef DRIVER_MODE +#define DRIVER_MODE +#endif + +#include "sqlite.h" + +#define FMAP_USERCERTS +#include "fieldmapping.h" + +void xmlReplaceChars(xmlChar *str, char s, char r); + +xmlDoc *eDBadminGetUserCertsList(eurephiaCTX *ctx, const char *sortkeys) { + xmlDoc *list_xml = NULL; + xmlNode *link_root_n = NULL, *link_n = NULL, *tmp_n = NULL; + dbresult *res = NULL; + xmlChar tmp[2050]; + char *dbsort = NULL; + int i; + + DEBUG(ctx, 20, "Function call: eDBadminGetUserCertsList(ctx, '%s')", sortkeys); + assert( ctx != 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_usercerts, sortkeys); + } + + res = sqlite_query(ctx, + "SELECT uicid, ucs.uid AS uid, certid, ucs.registered AS registered," + " ucs.accessprofile AS accessprofile, access_descr," + " username, " + " common_name, organisation, email, digest, depth " + " FROM openvpn_usercerts ucs" + " LEFT JOIN openvpn_certificates USING(certid)" + " LEFT JOIN openvpn_accesses acc ON(ucs.accessprofile = acc.accessprofile)" + " LEFT JOIN openvpn_users u ON(u.uid = ucs.uid)%s%s", + (dbsort != NULL ? " ORDER BY ":""), + (dbsort != NULL ? dbsort : "")); + + if( res == NULL ) { + eurephia_log(ctx, LOG_ERROR, 0, "Could not query the certificate table"); + return NULL; + } + + memset(&tmp, 0, 2050); + eurephiaXML_CreateDoc(ctx, 1, "usercerts_links", &list_xml, &link_root_n); + xmlStrPrintf(tmp, 64, (xmlChar *) "%i", sqlite_get_numtuples(res)); + xmlNewProp(link_root_n, (xmlChar *) "link_count", (xmlChar *) tmp); + + for( i = 0; i < sqlite_get_numtuples(res); i++ ) { + link_n = xmlNewChild(link_root_n, NULL, (xmlChar *) "usercert_link", NULL); + + sqlite_xml_value(link_n, XML_ATTR, "uicid", res, i, 0); + sqlite_xml_value(link_n, XML_ATTR, "registered", res, i, 3); + + tmp_n = sqlite_xml_value(link_n, XML_NODE, "username", res, i, 6); + sqlite_xml_value(tmp_n, XML_ATTR, "uid", res, i, 1); + + tmp_n = xmlNewChild(link_n, NULL, (xmlChar *) "certificate", NULL); + sqlite_xml_value(tmp_n, XML_ATTR, "certid", res, i, 2); + sqlite_xml_value(tmp_n, XML_ATTR, "depth", res, i, 11); + + xmlStrPrintf(tmp, 2048, (xmlChar *) "%.2048s", sqlite_get_value(res, i, 7)); + xmlReplaceChars(tmp, '_', ' '); + xmlNewChild(tmp_n, NULL, (xmlChar *) "common_name", tmp); + + xmlStrPrintf(tmp, 2048, (xmlChar *) "%.2048s", sqlite_get_value(res, i, 8)); + xmlReplaceChars(tmp, '_', ' '); + xmlNewChild(tmp_n, NULL, (xmlChar *) "organisation", tmp); + + sqlite_xml_value(tmp_n, XML_NODE, "email", res, i, 9); + sqlite_xml_value(tmp_n, XML_NODE, "digest", res, i, 10); + + tmp_n = sqlite_xml_value(link_n, XML_NODE, "access_profile", res, i, 5); + sqlite_xml_value(tmp_n, XML_ATTR, "accessprofile", res, i, 4); + } + sqlite_free_results(res); + + return list_xml; +} + + +int eDBadminUpdateUserCertLink(eurephiaCTX *ctx, xmlDoc *usrcrt_xml) { + dbresult *res = NULL; + xmlNode *usrcrt_n = NULL, *fmap_n = NULL; + eDBfieldMap *usrcrt_m = NULL; + char *mode = NULL; + int rc = 0; + + DEBUG(ctx, 20, "Function call: eDBadminUpdateUserCertLink(ctx, xmlDoc)"); + assert( (ctx != NULL) && (usrcrt_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; + } + + usrcrt_n = eurephiaXML_getRoot(ctx, usrcrt_xml, "usercerts_link", 1); + if( usrcrt_n == NULL ) { + eurephia_log(ctx, LOG_ERROR, 0, "Could not find a valid XML for the user-certs link request"); + return 0; + } + mode = xmlGetAttrValue(usrcrt_n->properties, "mode"); + if( mode == NULL ) { + eurephia_log(ctx, LOG_ERROR, 0, "Invalid user-cert link request (1)."); + return 0; + } + + fmap_n = xmlFindNode(usrcrt_n, "fieldMapping"); + if( fmap_n == NULL ) { + eurephia_log(ctx, LOG_ERROR, 0, "Invalid user-cert link request (2)."); + return 0; + } + usrcrt_m = eDBxmlMapping(ctx, tbl_sqlite_usercerts, NULL, fmap_n); + assert(usrcrt_m != NULL); + + if( strcmp(mode, "register") == 0 ) { + res = sqlite_query_mapped(ctx, SQL_INSERT, "INSERT INTO openvpn_usercerts", usrcrt_m, NULL, NULL); + rc = res->last_insert_id; + } else if( strcmp(mode, "remove") == 0 ) { + res = sqlite_query_mapped(ctx, SQL_DELETE, "DELETE FROM openvpn_usercerts", NULL, usrcrt_m, NULL); + rc = 1; + } + + if( res == NULL ) { + eurephia_log(ctx, LOG_ERROR, 0, "Failed to register user account / certificate"); + rc = -1; + } else { + sqlite_free_results(res); + } + + eDBfreeMapping(usrcrt_m); + return rc; +} |
