summaryrefslogtreecommitdiffstats
path: root/database/eurephiadb_mapping.c
blob: 4454e1b9124c190d6816cede22f5de240407155f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* eurephiadb_common.c  --  Common database functions, used by drivers
 *
 *  GPLv2 - Copyright (C) 2008  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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define EUREPHIADB_MAPPING_C
#include <eurephia_context.h>
#include <eurephia_admin_struct.h>
#include <eurephiadb_mapping.h>
#include <eurephia_nullsafe.h>
#include <passwd.h>


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;
}