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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
#include "netcosm.h"
#define SALT_LEN 12
#define ALGO GCRY_MD_SHA512
#define HASH_ITERS 10
static bool valid_login_name(const char *name);
/* returns a pointer to a malloc-allocated buffer containing the salted hex hash of pass */
/* salt should point to a buffer containing SALT_LEN random characters */
/* pass must be null-terminated */
static char *hash_pass_hex(const char *pass, const char *salt)
{
size_t pass_len = strlen(pass);
char *salted = gcry_malloc_secure(pass_len + SALT_LEN + 1);
memcpy(salted, salt, SALT_LEN);
memcpy(salted + SALT_LEN, pass, pass_len);
salted[pass_len + SALT_LEN] = '\0';
unsigned int hash_len = gcry_md_get_algo_dlen(ALGO);
unsigned char *hash = gcry_malloc_secure(hash_len);
gcry_md_hash_buffer(ALGO, hash, salted, pass_len + SALT_LEN);
unsigned char *tmp = gcry_malloc_secure(hash_len);
/* now hash the hash half a million times to slow things down */
for(int i = 0; i < HASH_ITERS - 1; ++i)
{
memcpy(tmp, hash, hash_len);
gcry_md_hash_buffer(ALGO, hash, tmp, hash_len);
}
gcry_free(tmp);
memset(salted, 0, pass_len + SALT_LEN + 1);
gcry_free(salted);
/* convert to hex */
char *hex = malloc(hash_len * 2 + 1);
char *ptr = hex;
for(unsigned int i = 0; i < hash_len; ++i, ptr += 2)
snprintf(ptr, 3, "%02x", hash[i]);
gcry_free(hash);
return hex;
}
static void add_user_append(int fd, const char *name, const char *pass, int authlevel)
{
char salt[SALT_LEN + 1];
for(int i = 0; i < SALT_LEN; ++i)
{
salt[i] = 'A' + rand()%26;
}
salt[SALT_LEN] = '\0';
char *hex = hash_pass_hex(pass, salt);
/* write */
flock(fd, LOCK_EX);
if(dprintf(fd, "%s:%s:%s:%d\n", name, salt, hex, authlevel) < 0)
perror("dprintf");
flock(fd, LOCK_UN);
close(fd);
free(hex);
}
/* writes the contents of USERFILE to a temp file, and return its path, which is statically allocated */
static int remove_user_internal(const char *user, int *found, char **filename)
{
FILE *in_fd = fopen(USERFILE, "a+");
static char tmp[32];
const char *template = "userlist_tmp.XXXXXX";
strncpy(tmp, template, sizeof(tmp));
int out_fd = mkstemp(tmp);
if(found)
*found = 0;
if(filename)
*filename = tmp;
while(1)
{
char *line = NULL;
char *junk;
size_t buflen = 0;
ssize_t len = getline(&line, &buflen, in_fd);
/* getline's return value is the actual length of the line read */
/* it's second argument in fact stores the length of the /buffer/, not the line */
if(len < 0)
{
free(line);
break;
}
char *old = strdup(line);
char *user_on_line = strtok_r(line, ":\r\n", &junk);
if(strcmp(user_on_line, user) != 0)
{
write(out_fd, old, len);
}
else
if(found)
(*found)++;
free(line);
free(old);
}
fclose(in_fd);
return out_fd;
}
bool auth_remove(const char *user2)
{
char *user = strdup(user2);
strtok(user, "\r\n");
if(valid_login_name(user))
{
int found = 0;
char *tmp;
remove_user_internal(user, &found, &tmp);
free(user);
if(found)
{
rename(tmp, USERFILE);
return true;
}
else
{
remove(tmp);
return false;
}
}
else
{
free(user);
return false;
}
}
bool add_change_user(const char *user2, const char *pass2, int level)
{
char *user = strdup(user2);
strtok(user, "\r\n");
char *pass = strdup(pass2);
strtok(pass, "\r\n");
printf("Add user '%s'\n", user);
if(!valid_login_name(user))
{
free(user);
free(pass);
return false;
}
/* remove any instances of the user in the file, write to temp file */
char *tmp;
int out_fd = remove_user_internal(user, NULL, &tmp);
/* add user to end of temp file */
add_user_append(out_fd, user, pass, level);
close(out_fd);
free(user);
memset(pass, 0, strlen(pass));
free(pass);
/* rename temp file -> user list */
rename(tmp, USERFILE);
return true;
}
static bool valid_login_name(const char *name)
{
while(*name)
{
char c = *name++;
switch(c)
{
case ' ':
case ':':
case '\n':
case '\r':
case '\t':
return false;
default:
break;
}
}
return true;
}
void first_run_setup(void)
{
printf("Welcome to NetCosm!\n");
printf("Please set up the administrator account now.\n");
char *admin_name;
size_t len = 0;
do {
admin_name = NULL;
printf("Admin account name: ");
fflush(stdout);
getline(&admin_name, &len, stdin);
strtok(admin_name, "\r\n");
} while(!valid_login_name(admin_name));
printf("Admin password (_DO_NOT_ USE A VALUABLE PASSWORD): ");
fflush(stdout);
char *admin_pass = NULL;
len = 0;
getline(&admin_pass, &len, stdin);
strtok(admin_pass, "\r\n");
if(!add_change_user(admin_name, admin_pass, PRIV_ADMIN))
error("Unknown error");
/* zero the memory */
memset(admin_name, 0, strlen(admin_name));
memset(admin_pass, 0, strlen(admin_pass));
free(admin_name);
free(admin_pass);
}
struct authinfo_t auth_check(const char *name2, const char *pass2)
{
/* get our own copy to remove newlines */
char *name = strdup(name2);
char *pass = strdup(pass2);
strtok(name, "\r\n");
strtok(pass, "\r\n");
/* find it in the user list */
FILE *f = fopen(USERFILE, "r");
flock(fileno(f), LOCK_SH);
struct authinfo_t ret;
ret.success = false;
ret.authlevel = PRIV_NONE;
while(1)
{
char *line = NULL;
size_t len = 0;
if(getline(&line, &len, f) < 0)
{
free(line);
goto bad;
}
if(!strcmp(strtok(line, ":\r\n"), name))
{
free(name);
char *salt = strdup(strtok(NULL, ":\r\n"));
char *hash = strdup(strtok(NULL, ":\r\n"));
ret.authlevel = strtol(strtok(NULL, ":\r\n"), NULL, 10);
free(line);
unsigned int hash_len = gcry_md_get_algo_dlen(ALGO);
if(strlen(hash) != hash_len * 2)
error("hash corrupt %d %d", strlen(hash), hash_len * 2);
if(strlen(salt) != SALT_LEN)
error("salt corrupt");
char *hex = hash_pass_hex(pass, salt);
memset(pass, 0, strlen(pass));
free(pass);
free(salt);
if(!memcmp(hex, hash, hash_len * 2))
{
free(hex);
free(hash);
ret.success = true;
goto good;
}
else
{
free(hex);
free(hash);
ret.success = false;
goto bad;
}
}
}
good:
printf("Successful authentication.\n");
return ret;
bad:
sleep(2);
printf("Failed authentication.\n");
return ret;
}
|