/* eurephiadb_common.c -- Common database functions, used by drivers * * GPLv2 - Copyright (C) 2008 David Sommerseth * * 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 #include #include #include #define EUREPHIADB_MAPPING_C #include #include #include #include #include eDBfieldMap *eDBprepareFieldMap(eurephiaCTX *ctx, int table, 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->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; } eDBfreeMapping(p->next); // Release value and this pointer. // Do not attempt to release field_name, as it is a constant char * free_nullsafe(p->value); free(p); } eDBfieldMap *eDBgetTableFieldMapping(int table) { eDBfieldMap *map; switch( table ) { case TABLE_USERS: map = eTblMap_user; break; case TABLE_CERTS: map = eTblMap_certificates; break; case TABLE_LASTLOG: map = eTblMap_lastlog; case TABLE_ATTEMPTS: map = eTblMap_attempts; case TABLE_BLACKLIST: map = eTblMap_blacklist; default: map = NULL; } return map; } void eDBcopyMapAttribs(eDBfieldMap *newmap, eDBfieldMap *dbmap, int field) { int i = 0; for( i = 0; dbmap[i].field_name != NULL; i++ ) { if( dbmap[i].field_id == field ) { newmap->field_name = dbmap[i].field_name; newmap->field_type = dbmap[i].field_type; } } } eDBfieldMap *eDBmkMapping_USERINFO(eurephiaCTX *ctx, eDBfieldMap *dbmap, 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}; map = eDBprepareFieldMap(ctx, TABLE_USERS, fields); assert( map != NULL ); // 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; } 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; } } return map; } char *eDBmkSortKeyString(eDBfieldMap *tfmap, const char *skeys_str) { eDBfieldMap *sk_map = NULL; int i, j; char *cp = NULL, *tok = NULL, *delims = ","; static char sortkeys[8194]; if( skeys_str == NULL ) { return NULL; } // Make sure we have table field map assert( tfmap != NULL ); // Get the correct table mapping for user input sk_map = eDBgetTableFieldMapping(tfmap[0].tableid); assert( sk_map != NULL ); // Split up the skeys_str (sort keys string) and build up a map cp = strdup_nullsafe(skeys_str); tok = strtok(cp, delims); memset(&sortkeys, 0, 8194); while( tok != NULL ) { for( i = 0; sk_map[i].field_id != FIELD_NONE; i++ ) { // If we find the the field in the unified mapping table ... if( strcmp(tok, sk_map[i].field_name) == 0 ) { // look up the proper field name for the current database for( j = 0; tfmap[j].field_name != 0; j++ ) { if( sk_map[i].field_id == tfmap[j].field_id ) { strncat(sortkeys, tfmap[j].field_name, (8192-strlen(sortkeys)-2)); strcat(sortkeys,","); } } } } tok = strtok(NULL, delims); } free_nullsafe(cp); sortkeys[strlen(sortkeys)-1] = '\0'; return sortkeys; }