-
Notifications
You must be signed in to change notification settings - Fork 4
/
faction.c
147 lines (124 loc) · 3.35 KB
/
faction.c
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
/*
* Atlantis v1.0 13 September 1993
* Copyright 1993 by Russell Wallace
*
* This program may be freely used, modified and distributed. It may not be
* sold or used commercially without prior written permission from the author.
*/
#include "faction.h"
#include "atlantis.h"
#include "battle.h"
#include <quicklist.h>
#include <mtrand.h>
#include <md5.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
struct quicklist *factions;
faction * create_faction(int no)
{
faction * f = (faction *)calloc(1, sizeof(faction));
if (f) {
f->no = no;
ql_push(&factions, f);
}
return f;
}
void free_faction(faction *f) {
free(f->name_);
free(f->addr_);
free(f->pwhash_);
ql_foreach(f->messages, free);
ql_free(f->messages);
ql_foreach(f->events, free);
ql_free(f->events);
ql_foreach(f->mistakes, free);
ql_free(f->mistakes);
/*
ql_foreach(f->allies.factions, free);
ql_free(f->allies.factions);
*/
ql_foreach(f->battles, (void (*)(void *))free_battle);
ql_free(f->battles);
ql_free(f->allies.factions);
ql_foreach(f->accept, free);
ql_free(f->accept);
ql_foreach(f->admit, free);
ql_free(f->admit);
free(f);
}
void faction_setname(faction * f, const char * name) {
if (name) {
f->name_ = (char *)realloc(f->name_, strlen(name)+1);
strcpy(f->name_, name);
} else {
free(f->name_);
f->name_ = 0;
}
}
const char * faction_getname(const faction * f) {
return f->name_;
}
void faction_setaddr(faction * f, const char * addr) {
if (addr) {
f->addr_ = (char *)realloc(f->addr_, strlen(addr)+1);
strcpy(f->addr_, addr);
} else {
free(f->addr_);
f->addr_ = 0;
}
}
const char * faction_getaddr(const faction * f) {
return f->addr_;
}
void faction_setpwhash(faction * f, const char * pwhash) {
if (pwhash) {
f->pwhash_ = (char *)realloc(f->pwhash_, strlen(pwhash)+1);
strcpy(f->pwhash_, pwhash);
} else {
free(f->pwhash_);
f->pwhash_ = 0;
}
}
const char * faction_getpwhash(const faction * f) {
return f->pwhash_;
}
void hex_encode(unsigned char * in, size_t inlen, char * out) {
size_t i;
for (i=0;i!=inlen;++i, out+=2) {
sprintf(out, "%02x", in[i]);
}
}
void faction_setpassword(faction * f, const char * password) {
if (password) {
char pwhash[64];
int salt = genrand_int31() % 0x10000;
md5_state_t pms;
md5_byte_t digest[16];
sprintf(pwhash, "%04x", salt);
md5_init(&pms);
md5_append(&pms, (const md5_byte_t *)pwhash, 4);
md5_append(&pms, (const md5_byte_t *)password, strlen(password));
md5_finish(&pms, digest);
hex_encode(digest, sizeof(digest), pwhash+4);
faction_setpwhash(f, pwhash);
} else {
faction_setpwhash(f, 0);
}
}
bool faction_checkpassword(struct faction *f, const char * password)
{
char buffer[64];
md5_state_t pms;
md5_byte_t digest[16];
if (f->pwhash_) {
md5_init(&pms);
md5_append(&pms, (const md5_byte_t *)f->pwhash_, 4);
md5_append(&pms, (const md5_byte_t *)password, strlen(password));
md5_finish(&pms, digest);
hex_encode(digest, sizeof(digest), buffer);
return strcmp(buffer, f->pwhash_+4)==0;
}
return false;
}