--- /dev/null
+#include <stdlib.h>
+
+#include "../lib/sstring.h"
+#include "../nick/nick.h" /* NICKLEN */
+#include "trusts.h"
+
+trustgroup *tglist;
+
+void trusts_freeall(void) {
+ trustgroup *tg, *ntg;
+ trusthost *th, *nth;
+
+ for(tg=tglist;tg;tg=ntg) {
+ ntg = tg->next;
+ for(th=tg->hosts;th;th=nth) {
+ nth = th->next;
+
+ th_free(th);
+ }
+
+ tg_free(tg);
+ }
+
+ tglist = NULL;
+}
+
+trustgroup *tg_getbyid(unsigned int id) {
+ trustgroup *tg;
+
+ for(tg=tglist;tg;tg=tg->next)
+ if(tg->id == id)
+ return tg;
+
+ return NULL;
+}
+
+void th_free(trusthost *th) {
+ freesstring(th->host);
+ free(th);
+}
+
+int th_add(trustgroup *tg, char *host, unsigned int maxseen, time_t lastseen) {
+ trusthost *th = malloc(sizeof(trusthost));
+ if(!th)
+ return 0;
+
+ th->host = getsstring(host, TRUSTHOSTLEN);
+ if(!th->host) {
+ th_free(th);
+ return 0;
+ }
+ th->maxseen = maxseen;
+ th->lastseen = lastseen;
+
+ th->next = tg->hosts;
+ tg->hosts = th;
+
+ return 1;
+}
+
+void tg_free(trustgroup *tg) {
+ freesstring(tg->name);
+ freesstring(tg->createdby);
+ freesstring(tg->contact);
+ freesstring(tg->comment);
+ free(tg);
+}
+
+int tg_add(unsigned int id, char *name, unsigned int trustedfor, int mode, unsigned int maxperident, unsigned int maxseen, time_t expires, time_t lastseen, time_t lastmaxuserreset, char *createdby, char *contact, char *comment) {
+ trustgroup *tg = malloc(sizeof(trustgroup));
+ if(!tg)
+ return 0;
+
+ tg->name = getsstring(name, TRUSTNAMELEN);
+ tg->createdby = getsstring(createdby, NICKLEN);
+ tg->contact = getsstring(contact, CONTACTLEN);
+ tg->comment = getsstring(comment, COMMENTLEN);
+ if(!tg->name || !tg->createdby || !tg->contact || !tg->comment) {
+ tg_free(tg);
+ return 0;
+ }
+
+ tg->id = id;
+ tg->trustedfor = trustedfor;
+ tg->mode = mode;
+ tg->maxperident = maxperident;
+ tg->maxseen = maxseen;
+ tg->expires = expires;
+ tg->lastseen = lastseen;
+ tg->lastmaxuserreset = lastmaxuserreset;
+
+ tg->next = tglist;
+ tglist = tg;
+
+ return 1;
+}
}
static void trusts_freedb(void) {
- trustgroup *tg, *ntg;
- trusthost *th, *nth;
-
- for(tg=tglist;tg;tg=ntg) {
- ntg = tg->next;
- for(th=tg->hosts;th;th=nth) {
- nth = th->next;
-
- freesstring(th->host);
- free(th);
- }
-
- freesstring(tg->name);
- freesstring(tg->createdby);
- freesstring(tg->contact);
- freesstring(tg->comment);
- free(tg);
- }
+ trusts_freeall();
trustsdbloaded = 0;
- tglist = NULL;
tgmaxid = 0;
}
-trustgroup *tg_getbyid(unsigned int id) {
- trustgroup *tg;
-
- for(tg=tglist;tg;tg=tg->next)
- if(tg->id == id)
- return tg;
-
- return NULL;
-}
-
static void loadhosts_data(const DBAPIResult *result, void *tag) {
if(!result) {
loaderror = 1;
while(result->next(result)) {
unsigned int groupid;
- trusthost *th;
+ char *host;
+ unsigned int maxseen, lastseen;
trustgroup *tg;
groupid = strtoul(result->get(result, 0), NULL, 10);
continue;
}
- th = malloc(sizeof(trusthost));
- if(!th)
- continue;
-
- th->host = getsstring(result->get(result, 1), TRUSTHOSTLEN);
- th->maxseen = strtoul(result->get(result, 2), NULL, 10);
- th->lastseen = (time_t)strtoul(result->get(result, 3), NULL, 10);
+ maxseen = strtoul(result->get(result, 2), NULL, 10);
+ lastseen = (time_t)strtoul(result->get(result, 3), NULL, 10);
+ host = result->get(result, 1);
- th->next = tg->hosts;
- tg->hosts = th;
+ if(!th_add(tg, host, maxseen, lastseen))
+ Error("trusts", ERR_WARNING, "Error adding host to trust %d: %s", groupid, host);
}
result->clear(result);
while(result->next(result)) {
unsigned int id;
- trustgroup *tg;
+ sstring *name, *createdby, *contact, *comment;
+ unsigned int trustedfor, mode, maxperident, maxseen;
+ time_t expires, lastseen, lastmaxuserreset;
id = strtoul(result->get(result, 0), NULL, 10);
if(id > tgmaxid)
tgmaxid = id;
- tg = malloc(sizeof(trustgroup));
- if(!tg)
- continue;
+ name = getsstring(result->get(result, 1), TRUSTNAMELEN);
+ trustedfor = strtoul(result->get(result, 2), NULL, 10);
+ mode = atoi(result->get(result, 3));
+ maxperident = strtoul(result->get(result, 4), NULL, 10);
+ maxseen = strtoul(result->get(result, 5), NULL, 10);
+ expires = (time_t)strtoul(result->get(result, 6), NULL, 10);
+ lastseen = (time_t)strtoul(result->get(result, 7), NULL, 10);
+ lastmaxuserreset = (time_t)strtoul(result->get(result, 8), NULL, 10);
+ createdby = getsstring(result->get(result, 9), NICKLEN);
+ contact = getsstring(result->get(result, 10), CONTACTLEN);
+ comment = getsstring(result->get(result, 11), COMMENTLEN);
+
+ if(name && createdby && contact && comment) {
+ if(!tg_add(id, name->content, trustedfor, mode, maxperident, maxseen, expires, lastseen, lastmaxuserreset, createdby->content, contact->content, comment->content))
+ Error("trusts", ERR_WARNING, "Error adding trustgroup %d: %s", id, name->content);
+ } else {
+ Error("trusts", ERR_ERROR, "Error allocating sstring in group loader, id: %d", id);
+ }
- tg->id = id;
- tg->name = getsstring(result->get(result, 1), TRUSTNAMELEN);
- tg->trustedfor = strtoul(result->get(result, 2), NULL, 10);
- tg->mode = atoi(result->get(result, 3));
- tg->maxperident = strtoul(result->get(result, 4), NULL, 10);
- tg->maxseen = strtoul(result->get(result, 5), NULL, 10);
- tg->expires = (time_t)strtoul(result->get(result, 6), NULL, 10);
- tg->lastseen = (time_t)strtoul(result->get(result, 7), NULL, 10);
- tg->lastmaxuserreset = (time_t)strtoul(result->get(result, 8), NULL, 10);
- tg->createdby = getsstring(result->get(result, 9), NICKLEN);
- tg->contact = getsstring(result->get(result, 10), CONTACTLEN);
- tg->comment = getsstring(result->get(result, 11), COMMENTLEN);
-
- tg->hosts = NULL;
-
- tg->next = tglist;
- tglist = tg;
+ freesstring(name);
+ freesstring(createdby);
+ freesstring(contact);
+ freesstring(comment);
}
result->clear(result);
struct trustmigration;
-typedef void (*TrustMigrationGroup)(void *, unsigned int, char *, unsigned int, unsigned int, unsigned int, unsigned int, time_t, time_t, time_t, char *, char *, char *);
-typedef void (*TrustMigrationHost)(void *, unsigned int, char *, unsigned int, time_t);
-typedef void (*TrustMigrationFini)(void *, int);
-
-typedef struct trustmigration {
- int count, cur;
- void *schedule;
- void *tag;
-
- TrustMigrationGroup group;
- TrustMigrationHost host;
- TrustMigrationFini fini;
-} trustmigration;
-
-typedef void (*TrustDBMigrationCallback)(int, void *);
-
typedef struct trusthost {
sstring *host;
unsigned int maxseen;
struct trustgroup *next;
} trustgroup;
-void trusts_reloaddb(void);
+/* trusts.c */
+char *trusts_timetostr(time_t);
+/* db.c */
extern int trustsdbloaded;
+void trusts_reloaddb(void);
+
+/* data.c */
extern trustgroup *tglist;
+void trusts_freeall(void);
+trustgroup *tg_getbyid(unsigned int);
+void th_free(trusthost *);
+int th_add(trustgroup *, char *, unsigned int, time_t);
+void tg_free(trustgroup *);
+int tg_add(unsigned int, char *, unsigned int, int, unsigned int, unsigned int, time_t, time_t, time_t, char *, char *, char *);
+
+/* migration.c */
+typedef void (*TrustMigrationGroup)(void *, unsigned int, char *, unsigned int, unsigned int, unsigned int, unsigned int, time_t, time_t, time_t, char *, char *, char *);
+typedef void (*TrustMigrationHost)(void *, unsigned int, char *, unsigned int, time_t);
+typedef void (*TrustMigrationFini)(void *, int);
-char *trusts_timetostr(time_t t);
+typedef struct trustmigration {
+ int count, cur;
+ void *schedule;
+ void *tag;
+
+ TrustMigrationGroup group;
+ TrustMigrationHost host;
+ TrustMigrationFini fini;
+} trustmigration;
+
+/* db-migration.c */
+
+typedef void (*TrustDBMigrationCallback)(int, void *);
#endif