summaryrefslogtreecommitdiffstats
path: root/common/eurephia_admin_common.c
blob: 72cf057f3fe9f27a6d5f74481959beaa90f3e4a3 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* 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_struct.h"
#include "eurephia_admin_common.h"


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_nullsafe(uname);
        newrec->password      = (pwd == NULL ? NULL : passwdhash(pwd));
        newrec->activated     = strdup_nullsafe(activated);
        newrec->deactivated   = strdup_nullsafe(deactivd);
        newrec->last_accessed = strdup_nullsafe(lastacc);
        newrec->next          = NULL;

        return newrec;
}


void _eAdminFreeUSERINFO_func(eurephiaUSERINFO *p) {
        if( p == NULL ) {
                return;
        }
        eAdminFreeCERTLIST(p->certlist);
        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(p);
}

void _eAdminFreeUSERLIST_func(eurephiaUSERLIST *p) {
        if( p == NULL ) {
                return;
        }

        eAdminFreeUSERINFO(p->users);
        free(p);
}


eurephiaCERTLIST *eAdminCreateCERTLIST() {
        eurephiaCERTLIST *lst = NULL;

        lst = (eurephiaCERTLIST *) malloc(sizeof(eurephiaCERTLIST)+2);
        memset(lst, 0, sizeof(eurephiaCERTLIST)+2);
        lst->num_certs = 0;
        return lst;
}

eurephiaCERTINFO *eAdminPopulateCERTINFO(int certid, int depth, const char *digest,
                                         const char *cname, const char *org, const char *email, const char *reg)
{
        eurephiaCERTINFO *newrec = NULL;

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

        newrec->certid        = certid;
        newrec->depth         = depth;
        newrec->digest        = strdup_nullsafe(digest);
        newrec->common_name   = strdup_nullsafe(cname);
        newrec->organisation  = strdup_nullsafe(org);
        newrec->email         = strdup_nullsafe(email);
        newrec->registered    = strdup_nullsafe(reg);
        newrec->next          = NULL;

        return newrec;
}

void eAdminInsertCERTINFO(eurephiaCERTLIST *list, eurephiaCERTINFO *cert) {
        assert( list != NULL );

        if( list->certs != NULL ) {
                cert->next = list->certs;
                list->certs = cert;
                list->num_certs++;
        } else {
                list->certs = cert;
                list->num_certs = 1;
        }
}

void _eAdminFreeCERTINFO_func(eurephiaCERTINFO *p) {
        if( p == NULL ) {
                return;
        }
        eAdminFreeCERTINFO(p->next);
        free_nullsafe(p->digest);
        free_nullsafe(p->common_name);
        free_nullsafe(p->organisation);
        free_nullsafe(p->email);
        free_nullsafe(p->registered);
        p->access = NULL;
        p->next = NULL;
        free(p);
}

void _eAdminFreeCERTLIST_func(eurephiaCERTLIST *p) {
#ifdef FIREWALL
        eurephiaACCESSLIST *aclst = NULL;
#endif
        if( p == NULL ) {
                return;
        }

#ifdef FIREWALL
        aclst = ((p->certs != NULL) && (p->certs->access != NULL)) ? p->certs->access->_head : NULL;
        eAdminFreeACCESSLIST(aclst);
#endif
        eAdminFreeCERTINFO(p->certs);
        free(p);
}

#ifdef FIREWALL
eurephiaACCESSINFO *eAdminRegisterACCESSINFO(eurephiaACCESSLIST *aclst, int accprofid,
                                             const char *fwprofile, const char *accdescr) {
        eurephiaACCESSINFO *ptr = NULL;

        assert( aclst != NULL );

        for( ptr = aclst->profiles; ptr != NULL; ptr = ptr->next) {
                if( ptr->accessprofile == accprofid ) {
                        return ptr;
                }
        }

        // If no record was found, register it automatically
        if( ptr == NULL ) {
                ptr = (eurephiaACCESSINFO *) malloc(sizeof(eurephiaACCESSINFO)+2);
                assert(ptr != NULL);
                memset(ptr, 0, sizeof(eurephiaACCESSINFO)+2);

                ptr->accessprofile = accprofid;
                ptr->fwprofile     = strdup_nullsafe(fwprofile);
                ptr->access_descr  = strdup_nullsafe(accdescr);
                ptr->_head         = aclst;

                if( aclst->profiles == NULL ) {
                        aclst->profiles = ptr;
                        aclst->num_profiles = 1;
                } else {
                        ptr->next = aclst->profiles;
                        aclst->profiles = ptr;
                        aclst->num_profiles++;
                }
        }
        return ptr;
}


eurephiaACCESSLIST *eAdminCreateACCESSLIST() {
        eurephiaACCESSLIST *ptr = NULL;

        ptr = (eurephiaACCESSLIST *) malloc(sizeof(eurephiaACCESSLIST)+2);
        assert(ptr != NULL);
        memset(ptr, 0, sizeof(eurephiaACCESSLIST)+2);
        return ptr;
}


void _eAdminFreeACCESSINFO_func(eurephiaACCESSINFO *p) {
        if( p == NULL ) {
                return;
        }
        eAdminFreeACCESSINFO(p->next);
        free_nullsafe(p->access_descr);
        free_nullsafe(p->fwprofile);
        p->_head = NULL;
        p->accessprofile = 0;
        free(p);
}

void _eAdminFreeACCESSLIST_func(eurephiaACCESSLIST *p) {
        if( p == NULL ) {
                return;
        }
        eAdminFreeACCESSINFO(p->profiles);
        p->num_profiles = 0;
        free(p);
}
#endif // FIREWALL