diff options
| author | David Sommerseth <dazo@users.sourceforge.net> | 2008-12-13 21:21:31 +0100 |
|---|---|---|
| committer | David Sommerseth <dazo@users.sourceforge.net> | 2008-12-13 21:21:31 +0100 |
| commit | 7a015bdf45d739ca3e6a4ac7d7e9fdb8daa7ab70 (patch) | |
| tree | 6ba3d99ba2a197def283155006f70169e9348d85 /database | |
| parent | e2a63248399aaff5605316702f9085ceda0ef8c5 (diff) | |
| download | eurephia-7a015bdf45d739ca3e6a4ac7d7e9fdb8daa7ab70.tar.gz eurephia-7a015bdf45d739ca3e6a4ac7d7e9fdb8daa7ab70.tar.xz eurephia-7a015bdf45d739ca3e6a4ac7d7e9fdb8daa7ab70.zip | |
Completed rewriting eDBmkMapping_USERINFO(...) to eDBxmlMapping(...)
This new mapping function uses a flexible XML document as input, and
will be generic to all other tables as well. XML structure is:
<fieldMapping table="{table name}>
<{fieldname}>{value}</{fieldname}>
</fieldMapping>
An example:
<fieldMapping table="users">
<uid>1</uid>
<username>testuser1</username>
</fieldMapping>
This function will return a eDBfieldMap structure contatining the proper
fieldnames with values assigned, depending on the database map given
when called.
Diffstat (limited to 'database')
| -rw-r--r-- | database/eurephiadb_mapping.c | 159 | ||||
| -rw-r--r-- | database/eurephiadb_mapping.h | 14 |
2 files changed, 109 insertions, 64 deletions
diff --git a/database/eurephiadb_mapping.c b/database/eurephiadb_mapping.c index 1a2f6c6..56913be 100644 --- a/database/eurephiadb_mapping.c +++ b/database/eurephiadb_mapping.c @@ -23,6 +23,8 @@ #include <string.h> #include <assert.h> +#include <libxml/tree.h> + #define EUREPHIADB_MAPPING_C #include <eurephia_context.h> #include <eurephia_admin_struct.h> @@ -31,27 +33,6 @@ #include <passwd.h> -eDBfieldMap *eDBprepareFieldMap(eurephiaCTX *ctx, int table, const char *tblalias, long fields[]) { - eDBfieldMap *map = NULL, *ptr = NULL; - int i = 0; - - for( i = 0; fields[i] != FIELD_NONE; i++ ) { - ptr = (eDBfieldMap *) malloc(sizeof(eDBfieldMap)+2); - memset(ptr, 0, sizeof(eDBfieldMap)+2); - - ptr->tableid = table; - ptr->table_alias = strdup_nullsafe(tblalias); - ptr->field_id = fields[i]; - ptr->field_name = NULL; - ptr->value = NULL; - - ptr->next = map; - map = ptr; - } - return map; -} - - void eDBfreeMapping(eDBfieldMap *p) { if( p == NULL ) { return; @@ -67,34 +48,52 @@ void eDBfreeMapping(eDBfieldMap *p) { eDBfieldMap *eDBgetTableFieldMapping(int table) { - eDBfieldMap *map; + eDBfieldMap *srcmap, *newmap = NULL, *ptr = NULL; + int i; switch( table ) { case TABLE_USERS: - map = eTblMap_user; + srcmap = eTblMap_user; break; case TABLE_CERTS: - map = eTblMap_certificates; + srcmap = eTblMap_certificates; break; case TABLE_LASTLOG: - map = eTblMap_lastlog; + srcmap = eTblMap_lastlog; case TABLE_ATTEMPTS: - map = eTblMap_attempts; + srcmap = eTblMap_attempts; case TABLE_BLACKLIST: - map = eTblMap_blacklist; + srcmap = eTblMap_blacklist; default: - map = NULL; + return NULL; } - return map; + + // Copy the mapping into a pointer chain + for( i = 0; srcmap[i].field_id != FIELD_NONE; i++ ) { + ptr = (eDBfieldMap *) malloc(sizeof(eDBfieldMap)+2); + assert(ptr != NULL); + memset(ptr, 0, sizeof(eDBfieldMap)+2); + + ptr->tableid = srcmap[i].tableid; + ptr->table_alias = NULL; + ptr->field_id = srcmap[i].field_id; + ptr->field_type = srcmap[i].field_type; + ptr->field_name = srcmap[i].field_name; + ptr->value = NULL; + ptr->next = newmap; + newmap = ptr; + } + + return newmap; } -void eDBcopyMapAttribs(eDBfieldMap *newmap, eDBfieldMap *dbmap, int field) { +inline void eDBcopyMapAttribs(eDBfieldMap *newmap, eDBfieldMap *dbmap, int field) { int i = 0; for( i = 0; dbmap[i].field_name != NULL; i++ ) { @@ -106,43 +105,83 @@ void eDBcopyMapAttribs(eDBfieldMap *newmap, eDBfieldMap *dbmap, int field) { } -eDBfieldMap *eDBmkMapping_USERINFO(eurephiaCTX *ctx, eDBfieldMap *dbmap, const char *tblalias, - eurephiaUSERINFO *user) -{ - eDBfieldMap *map = NULL, *ptr = NULL; - char *uid_str = NULL; - long fields[] = {FIELD_LASTACCESS, FIELD_DEACTIVATED, FIELD_ACTIVATED, - FIELD_PASSWD, FIELD_UNAME, FIELD_RECID, FIELD_NONE}; +eDBfieldMap *eDBxmlMapping(eurephiaCTX *ctx, eDBfieldMap *dbmap, const char *tblalias, xmlDoc *xmlmap) { + eDBfieldMap *map = NULL, *ptr = NULL, *ptr2 = NULL; + xmlNode *nptr = NULL; + int lvl = 0; + + // FIXME: Make sure xmlcrit's root nodes attribute table matches dbmap->tableid + + // Get a copy of the system map + map = eDBgetTableFieldMapping(dbmap->tableid); + + // Loop through the XML, and register the different values to the system map fields + for( nptr = xmlDocGetRootElement(xmlmap); nptr != NULL; ) { + // We are only interested in element nodes + if( (nptr->type != XML_ELEMENT_NODE) ) { + nptr = nptr->next; + continue; + } - map = eDBprepareFieldMap(ctx, TABLE_USERS, tblalias, fields); - assert( map != NULL ); + if( (lvl == 0) && (nptr->name != NULL) + && (xmlStrcmp(nptr->name, (xmlChar *)"fieldMapping") == 0) ) { + // Contiune parsing children notes of the root node + nptr = nptr->children; + lvl++; + } else if( (lvl == 1) && (nptr->name != NULL) && (nptr->children != NULL)) { + int setnull = 0; + xmlAttr *atr; + + // printf("Field '%s' - Value: '%s'\n", nptr->name, nptr->children->content); + + // Check if we have the "setnull" attribute set to "1" + for( atr = nptr->properties; atr != NULL; atr = atr->next ) { + if( (atr->name != NULL) && (xmlStrcmp(atr->name, (xmlChar *)"setnull") == 0) ) { + xmlNode *n = atr->children; + setnull = (((n != NULL) && (n->content != NULL) + && (xmlStrcmp(n->content,(xmlChar *)"1") == 0)) ? 1 : 0); + break; + } + } - // Convert uid from int to char * - if( user->uid != 0 ) { - uid_str = (char *) malloc(33); - snprintf(uid_str, 32, "%i%c", user->uid, 0); - } else { - uid_str = NULL; + // Look up field in our copy of the system map + for( ptr = map; ptr != NULL; ptr = ptr->next ) { + // If we find the field, copy the value into our map + if( xmlStrcmp((xmlChar *)ptr->field_name, nptr->name) == 0 ) { + if( setnull ) { + // If flagged for being set to NULL, change the field type + ptr->field_type = ft_SETNULL; + } + switch( ptr->field_type ) { + case ft_PASSWD: // If it is a password field type, hash the value + ptr->value = passwdhash((char *)nptr->children->content); + break; + + case ft_SETNULL: + // Don't set any values if we want to set it to NULL + ptr->value = NULL; + break; + default: + ptr->value = strdup_nullsafe((char *)nptr->children->content); + break; + } + break; + } + } + + // Go to next XML node + nptr = nptr->next; + } else { + printf("*** Illegal XML - unaccepted node: (%i) %s\n", nptr->type, nptr->name); + return NULL; + } } + // Make a reference to for( ptr = map; ptr != NULL; ptr = ptr->next ) { // Copy over field name - translated via the db mapping table eDBcopyMapAttribs(ptr, dbmap, ptr->field_id); - - // Copy over values - switch( ptr->field_id ) { - case FIELD_RECID: ptr->value = uid_str; break; - case FIELD_UNAME: ptr->value = strdup_nullsafe(user->username); break; - case FIELD_PASSWD: ptr->value = passwdhash(user->password); break; - case FIELD_ACTIVATED: ptr->value = strdup_nullsafe(user->activated); break; - case FIELD_DEACTIVATED: ptr->value = strdup_nullsafe(user->deactivated); break; - case FIELD_LASTACCESS: ptr->value = strdup_nullsafe(user->last_accessed); break; - } - - // Set ft_SETNULL if that flag is set for this field_id - if( (ptr->field_id & user->setnull_flags) == ptr->field_id ) { - ptr->field_type = ft_SETNULL; - } + ptr->table_alias = strdup_nullsafe(tblalias); } return map; } diff --git a/database/eurephiadb_mapping.h b/database/eurephiadb_mapping.h index 5079490..f92455b 100644 --- a/database/eurephiadb_mapping.h +++ b/database/eurephiadb_mapping.h @@ -24,12 +24,16 @@ #ifndef EUREPHIADB_MAPPING_H # define EUREPHIADB_MAPPING_H +#ifdef HAVE_LIBXML2 +# include <libxml/tree.h> +#endif + typedef enum eDBfieldType_e { ft_UNDEF, ft_INT, ft_STRING, ft_DATETIME, ft_PASSWD, ft_SETNULL } eDBfieldType; typedef struct _eDBfieldMap_s { int tableid; char *table_alias; - long field_id; + long field_id; eDBfieldType field_type; char *field_name; char *value; @@ -71,9 +75,9 @@ typedef struct _eDBfieldMap_s { #ifdef EUREPHIADB_MAPPING_C static eDBfieldMap eTblMap_user[] = { - {TABLE_USERS, NULL, FIELD_RECID, ft_INT, "id"}, {TABLE_USERS, NULL, FIELD_RECID, ft_INT, "uid"}, {TABLE_USERS, NULL, FIELD_UNAME, ft_STRING, "username"}, + {TABLE_USERS, NULL, FIELD_PASSWD, ft_PASSWD, "password"}, {TABLE_USERS, NULL, FIELD_ACTIVATED, ft_DATETIME, "activated"}, {TABLE_USERS, NULL, FIELD_DEACTIVATED, ft_DATETIME, "deactivated"}, {TABLE_USERS, NULL, FIELD_LASTACCESS, ft_DATETIME, "lastaccess"}, @@ -82,7 +86,7 @@ static eDBfieldMap eTblMap_user[] = { static eDBfieldMap eTblMap_certificates[] = { {TABLE_CERTS, NULL, FIELD_RECID, ft_INT, "id"}, - {TABLE_CERTS, NULL, FIELD_RECID, ft_INT, "certid"}, + {TABLE_CERTS, NULL, FIELD_CERTID, ft_INT, "certid"}, {TABLE_CERTS, NULL, FIELD_CERTDEPTH, ft_INT, "depth"}, {TABLE_CERTS, NULL, FIELD_CNAME, ft_STRING, "name"}, {TABLE_CERTS, NULL, FIELD_ORG, ft_STRING, "org"}, @@ -126,7 +130,9 @@ static eDBfieldMap eTblMap_blacklist[] = { #endif // #ifdef EUREPHIADB_MAPPING_C void eDBfreeMapping(eDBfieldMap *p); -eDBfieldMap *eDBmkMapping_USERINFO(eurephiaCTX *ctx, eDBfieldMap *dbmap, const char *alias, eurephiaUSERINFO *user); +#ifdef HAVE_LIBXML2 +eDBfieldMap *eDBxmlMapping(eurephiaCTX *ctx, eDBfieldMap *dbmap, const char *tblalias, xmlDoc *xmlmap); +#endif char *eDBmkSortKeyString(eDBfieldMap *tfmap, const char *skeys_str); #endif // !EUREPHIADB_MAPPING_H |
