summaryrefslogtreecommitdiffstats
path: root/common/eurephia_admin_common.c
blob: c49c1a3083c537b49ff47e06cc7ffbe671a4f4a8 (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
/* 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 <passwd.h>

#define EUREPHIA_ADMIN_COMMON_C
#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;
        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;
}

eurephiaUSERINFO *eAdminPopulateUSERINFO(int uid, const char *uname, const char *pwd,
                                         const char *activated, const char *deactivd, const char *lastacc)
{
        eurephiaUSERINFO *newrec = NULL;

        newrec = (eurephiaUSERINFO *) malloc(sizeof(eurephiaUSERINFO)+2);
        assert( newrec != NULL );
        memset(newrec, 0, sizeof(eurephiaUSERINFO)+2);

        newrec->uid           = uid;
        newrec->username      = strdup(uname);
        newrec->password      = (pwd == NULL ? NULL : passwdhash(pwd));
        newrec->activated     = strdup(activated);
        newrec->deactivated   = strdup(deactivd);
        newrec->last_accessed = strdup(lastacc);
        newrec->next          = NULL;

        return newrec;
}


void _eAdminFreeUSERINFO_func(eurephiaUSERINFO *p) {
        if( p == NULL ) {
                return;
        }
        eAdminFreeUSERINFO(p->next);
        free_nullsafe(p->username);
        free_nullsafe(p->password);
        free_nullsafe(p->activated);
        free_nullsafe(p->deactivated);
        free_nullsafe(p->last_accessed);
        p->next = NULL;
        free_nullsafe(p);
}