summaryrefslogtreecommitdiffstats
path: root/common/eurephia_admin_common.c
blob: 7333408ebad8136a1b5a28af54715f2ce55e7289 (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
/* eurephia_admin_common.c  --  Common functions used for the admin API
 *
 *  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>

#include <eurephia_nullsafe.h>

#include "eurephia_admin_common.h"

eFieldMap *eAdminGetTableMap(int table) {
        eFieldMap *map;

        switch( table ) {
        case TABLE_USERS:
                map = eSortkeys_user;
                break;

        case TABLE_CERTS:
                map = eSortkeys_certificates;
                break;

        case TABLE_LASTLOG:
                map = eSortkeys_lastlog;

        case TABLE_ATTEMPTS:
                map = eSortkeys_attempts;

        case TABLE_BLACKLIST:
                map = eSortkeys_blacklist;

        default:
                map = NULL;
        }
        return map;
}


char *eAdminConvertSortKeys(eFieldMap *tfmap, const char *skeys_str) {
        eFieldMap *sk_map = NULL;
        long sortmap = 0;
        int i, j;
        char *cp = NULL, *tok = NULL, *delims = ",";
        static char sortkeys[8194];

        // Make sure we have table field map
        assert( tfmap != NULL );

        // Get the correct table mapping for user input
        sk_map = eAdminGetTableMap(tfmap[0].tableid);
        assert( sk_map != NULL );

        // Split up the skeys_str (sort keys string) and build up a map
        cp = strdup(skeys_str);
        tok = strtok(cp, delims);
        memset(&sortkeys, 0, 8194);
        while( tok != NULL ) {
                for( i = 0; sk_map[i].fieldname != NULL; i++ ) {
                        // If we find the the field in the unified mapping table ...
                        if( strcmp(tok, sk_map[i].fieldname) == 0 ) {
                                // look up the proper field name for the current database
                                for( j = 0; tfmap[j].fieldname != 0; j++ ) {
                                        if( (sk_map[i].sortkeyid & tfmap[j].sortkeyid) == sk_map[i].sortkeyid ) {
                                                strncat(sortkeys, tfmap[j].fieldname, (8192-strlen(sortkeys)));
                                                strcat(sortkeys,",");
                                        }
                                }
                        }
                }
                tok = strtok(NULL, delims);
        }
        free_nullsafe(cp);
        sortkeys[strlen(sortkeys)-1] = '\0';
        return sortkeys;
}