summaryrefslogtreecommitdiffstats
path: root/auth.c
blob: c5c17edeb2597c2c3486cc35bc08586be42b2a0c (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
222
223
224
225
226
227
228
229
230
231
232
233
/*
 *  Sauerbraten coopedit helper bot
 *
 *  Copyright (C) 2007 Michael Buesch <mb@bu3sch.de>
 *
 *  This software is provided 'as-is', without any express or implied
 *  warranty.  In no event will the authors be held liable for any damages
 *  arising from the use of this software.
 *
 *  Permission is granted to anyone to use this software for any purpose,
 *  including commercial applications, and to alter it and redistribute it
 *  freely, subject to the following restrictions:
 *
 *  1. The origin of this software must not be misrepresented; you must not
 *     claim that you wrote the original software. If you use this software
 *     in a product, an acknowledgment in the product documentation would be
 *     appreciated but is not required.
 *  2. Altered source versions must be plainly marked as such, and must not be
 *     misrepresented as being the original software.
 *  3. This notice may not be removed or altered from any source distribution.
 */

#include "main.h"
#include "auth.h"

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <time.h>


static char random_char_from_array(const char *array, size_t len)
{
	return array[random_int() % len];
}

/* sizeof(buf) >= len + 1 */
static void gen_pronounceable_string(char *buf, size_t len)
{
	static const char *consonants = "bcdfghjklmnpqrstvwx"; /* no y,z */
	static const char *vowels = "aeiou";
	const size_t consonants_len = strlen(consonants);
	const size_t vowels_len = strlen(vowels);
	enum {
		TYPE_RANDOM, TYPE_CONSONANT, TYPE_VOWEL,
	} next_type;
	int nr_consonants = 0;
	int nr_vowels = 0;
	int rnd;
	size_t i;
	char c;

	for (i = 0; i < len; i++) {
		if (i == 1 && nr_consonants == 1) {
			/* Do not start with two consonants */
			next_type = TYPE_VOWEL;
			nr_consonants = 0;
		} else if (nr_vowels == 1) {
			next_type = TYPE_CONSONANT;
			nr_vowels = 0;
		} else if (nr_consonants == 2) {
			next_type = TYPE_VOWEL;
			nr_consonants = 0;
		} else
			next_type = TYPE_RANDOM;

		if (next_type == TYPE_RANDOM) {
			rnd = random_int() % 2;
			if (rnd == 0)
				next_type = TYPE_CONSONANT;
			else
				next_type = TYPE_VOWEL;
		}
		switch (next_type) {
		case TYPE_CONSONANT:
			c = random_char_from_array(consonants, consonants_len);
			buf[i] = c;
			nr_consonants++;
			break;
		case TYPE_VOWEL:
			c = random_char_from_array(vowels, vowels_len);
			buf[i] = c;
			nr_vowels++;
			break;
		case TYPE_RANDOM:
			/* Can't happen */
			fprintf(stderr, "TYPE_RANDOM\n");
			exit(1);
		}
	}
	buf[i] = '\0';
}

void gen_authmap(struct sbot_authmap *map)
{
	struct sbot_authkey *key;
	int i;

	for (i = 0; i < NR_AUTHKEYS; i++) {
		key = &(map->keys[i]);
		gen_pronounceable_string(key->key, AUTHKEY_LEN);
	}
	map->mapid = (random_int() & 0xFFFF);
}

int do_export_authmap(const struct sbot_authmap *map, FILE *fd)
{
	unsigned int i;
	time_t t;
	struct tm *loc;

	t = time(NULL);
	loc = localtime(&t);

	fprintf(fd, "# Sauerbot authentication map.\n");
	fprintf(fd, "# This is private information.\n");
	fprintf(fd, "#\n");
	fprintf(fd, "# %s", asctime(loc));
	fprintf(fd, "#\n");
	fprintf(fd, "\n");
	fprintf(fd, "MAPID: %04X\n", map->mapid);
	fprintf(fd, "\n");
	for (i = 0; i < NR_AUTHKEYS; i++) {
		if ((i % 5 == 0) && (i != 0))
			fprintf(fd, "\n");
		fprintf(fd, "%02u: %s    ", i, map->keys[i].key);
	}
	fprintf(fd, "\n");

	return 0;
}

int export_authmap(const struct sbot_authmap *map, const char *file)
{
	FILE *fd;
	int err;

	printf("Exporting authentication map...\n");
	fd = fopen(file, "w+");
	if (!fd) {
		fprintf(stderr, "Could not open authmap export file %s: %s\n",
			file, strerror(errno));
		return -1;
	}
	err = do_export_authmap(map, fd);
	if (err) {
		fprintf(stderr, "Failed to write authmap export file \"%s\"\n",
			file);\
		fclose(fd);
		return -1;
	}
	fclose(fd);

	return 0;
}

int do_import_authmap(struct sbot_authmap *map, FILE *fd)
{
	ssize_t len;
	size_t blen = 0;
	char *buf = NULL;
	unsigned int tmp, i;
	unsigned int a, b, c, d, e;
	char b1[21];
	char b2[21];
	char b3[21];
	char b4[21];
	char b5[21];

	memset(map, 0, sizeof(*map));
	while ((len = getline(&buf, &blen, fd)) > 0) {
		if (buf[0] == '#')
			continue;
		if (sscanf(buf, "MAPID: %04X", &tmp) == 1) {
			map->mapid = tmp;
			map->mapid_ok = 1;
			continue;
		}
		if (sscanf(buf, "%02u: %20s   %02u: %20s   %02u: %20s   %02u: %20s   %02u: %20s",
			   &a, b1, &b, b2, &c, b3, &d, b4, &e, b5) == 10) {
			if ((b != a + 1) || (c != a + 2) ||
			    (d != a + 3) || (e != a + 4))
				continue;
			if (e >= NR_AUTHKEYS)
				continue;
			strncpy(map->keys[a].key, b1, AUTHKEY_LEN);
			map->keys[a].ok = 1;
			strncpy(map->keys[b].key, b2, AUTHKEY_LEN);
			map->keys[b].ok = 1;
			strncpy(map->keys[c].key, b3, AUTHKEY_LEN);
			map->keys[c].ok = 1;
			strncpy(map->keys[d].key, b4, AUTHKEY_LEN);
			map->keys[d].ok = 1;
			strncpy(map->keys[e].key, b5, AUTHKEY_LEN);
			map->keys[e].ok = 1;
		}
	}
	if (!map->mapid_ok)
		fprintf(stderr, "Authmap import: Missing MAPID number\n");
	for (i = 0; i < NR_AUTHKEYS; i++) {
		if (!map->keys[i].ok) {
			fprintf(stderr, "Authmap import: Corrupt authmap data\n");
			memset(map, 0, sizeof(*map));
			return -1;
		}
	}

	return 0;
}

int import_authmap(struct sbot_authmap *map, const char *file)
{
	FILE *fd;
	int err;

	printf("Importing authentication map...\n");
	fd = fopen(file, "r");
	if (!fd) {
		fprintf(stderr, "Could not open authentication map %s: %s\n",
			file, strerror(errno));
		return -1;
	}
	err = do_import_authmap(map, fd);
	if (err) {
		fprintf(stderr, "Failed to parse authmap file \"%s\"\n",
			file);
		fclose(fd);
		return -1;
	}
	fclose(fd);

	return 0;
}
bues.ch cgit interface