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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
|
#include "common.h"
#include <termios.h>
#include <fcntl.h>
#include <unistd.h>
#include <openssl/rand.h>
#include <openssl/err.h>
struct string_builder passwd_log = {0};
uid_t ruid;
gid_t rgid;
uid_t euid;
gid_t egid;
char
encode6 (unsigned char b6)
{
if (b6 < ('9' - '.' + 1) ) return '.' + b6;
if (b6 < ('9' - '.' + 1) + ('Z' - 'A' + 1) ) return 'A' + b6 - ('9' - '.' + 1);
if (b6 < ('9' - '.' + 1) + ('Z' - 'A' + 1) + ('z' - 'a' + 1)) return 'a' + b6 - ('9' - '.' + 1) - ('Z' - 'A' + 1);
printf ("0x%X\n", b6);
assert (!"out of range");
return -1;
}
unsigned char
decode6 (char c)
{
if ('.' <= c && c <= '9') return c - '.';
if ('A' <= c && c <= 'Z') return c - 'A' + ('9' - '.' + 1);
if ('a' <= c && c <= 'z') return c - 'a' + ('9' - '.' + 1) + ('Z' - 'A' + 1);
assert (!"out of range");
return -1;
}
void
encode (char *into, const unsigned char* from, unsigned int size)
{
for (; size >= 3; size -= 3) {
// 012345670123456701234567
// 012345012345012345012345
into[0] = encode6 ( from[0] >> 2);
into[1] = encode6 ((from[0] & 0b00000011) << 4 | from[1] >> 4);
into[2] = encode6 ((from[1] & 0b00001111) << 2 | from[2] >> 6);
into[3] = encode6 ((from[2] & 0b00111111));
from += 3;
into += 4;
}
if (size == 2) {
into[0] = encode6 ( from[0] >> 2);
into[1] = encode6 ((from[0] & 0b00000011) << 4 | from[1] >> 4);
into[2] = encode6 ((from[1] & 0b00001111) << 2);
} else if (size == 1) {
into[0] = encode6 ( from[0] >> 2);
into[1] = encode6 ((from[0] & 0b00000011) << 4);
}
}
void
decode (unsigned char *into, struct string_view from)
{
size_t len;
for (len = SVLEN (from); len >= 4; len -= 4) {
// 012345012345012345012345
// 012345670123456701234567
unsigned char d1 = decode6 (from.begin[0]);
unsigned char d2 = decode6 (from.begin[1]);
unsigned char d3 = decode6 (from.begin[2]);
unsigned char d4 = decode6 (from.begin[3]);
into[0] = d1 << 2 | d2 >> 4;
into[1] = (d2 & 0b001111) << 4 | d3 >> 2;
into[2] = d3 << 6 | d4;
from.begin += 4;
into += 3;
}
if (len == 3) {
unsigned char d1 = decode6 (from.begin[0]);
unsigned char d2 = decode6 (from.begin[1]);
unsigned char d3 = decode6 (from.begin[2]);
into[0] = d1 << 2 | d2 >> 4;
into[1] = (d2 & 0b001111) << 4 | d3 >> 2;
} else if (len == 2) {
unsigned char d1 = decode6 (from.begin[0]);
unsigned char d2 = decode6 (from.begin[1]);
into[0] = d1 << 2 | d2 >> 4;
} else assert ("!unreachable");
}
void
generate_salt (unsigned char salt[PASSWORD_SALT_SIZE])
{
if (RAND_bytes (salt, PASSWORD_SALT_SIZE) != 1) {
DIE ("error generating salt: RAND_bytes: %s",
ERR_reason_error_string (ERR_get_error ()));
}
}
unsigned char *
hash_password (unsigned char hash[SHA_DIGEST_LENGTH],
struct string_view password,
const unsigned char salt[PASSWORD_SALT_SIZE])
{
EVP_MD_CTX *mdctx = EVP_MD_CTX_new ();
if (mdctx == NULL) {
DIE ("error hashing creating context: EVP_MD_CTX_new: %s",
ERR_reason_error_string (ERR_get_error ()));
}
if (EVP_DigestInit_ex (mdctx, EVP_sha1 (), NULL) != 1) {
DIE ("error initializing digest: EVP_DigestInit_ex: %s",
ERR_reason_error_string (ERR_get_error ()));
}
if (EVP_DigestUpdate (mdctx, password.begin, SVLEN (password)) != 1) {
DIE ("error updating digest with password: EVP_DigestUpdate: %s",
ERR_reason_error_string (ERR_get_error ()));
}
if (EVP_DigestUpdate (mdctx, salt, PASSWORD_SALT_SIZE) != 1) {
DIE ("error updating digest with salt: EVP_DigestUpdate: %s",
ERR_reason_error_string (ERR_get_error ()));
}
unsigned int hash_len;
if (EVP_DigestFinal_ex(mdctx, hash, &hash_len) != 1) {
DIE ("error finalizing digest: EVP_DigestFinal_ex: %s",
ERR_reason_error_string (ERR_get_error ()));
}
EVP_MD_CTX_free (mdctx);
return hash;
}
struct string_view
encode_password (struct string_view password)
{
static char encoded[PASSWORD_ENCODED_LENGTH + 1] = {
[0] = '$',
[1] = '1',
[2] = '$',
[3 + PASSWORD_SALT_LENGTH] = '$',
};
char *salt_encoded = (void *) &encoded[3];
char *hash_encoded = (void *) &encoded[3 + PASSWORD_SALT_LENGTH + 1];
unsigned char salt[PASSWORD_SALT_SIZE];
generate_salt (salt);
encode (salt_encoded, salt, sizeof (salt));
unsigned char hash[SHA_DIGEST_LENGTH];
hash_password (hash, password, salt);
encode (hash_encoded, hash, SHA_DIGEST_LENGTH);
return (struct string_view) { encoded, encoded + PASSWORD_ENCODED_LENGTH };
}
bool
check_password (struct string_view password,
struct string_view encoded_password)
{
if (*encoded_password.begin++ != '$') return false;
if (*encoded_password.begin++ != '1') return false;
if (*encoded_password.begin++ != '$') return false;
struct string_view encoded_salt = {
.begin = encoded_password.begin,
.end = encoded_password.begin += PASSWORD_SALT_LENGTH,
};
if (*encoded_password.begin++ != '$') return false;
struct string_view encoded_hash = {
.begin = encoded_password.begin,
.end = encoded_password.begin + PASSWORD_HASH_LENGTH,
};
unsigned char decoded_salt[PASSWORD_SALT_SIZE];
decode (decoded_salt, encoded_salt);
unsigned char hash[SHA_DIGEST_LENGTH];
hash_password (hash, password, decoded_salt);
unsigned char decoded_hash[SHA_DIGEST_LENGTH];
decode (decoded_hash, encoded_hash);
return memcmp (hash, decoded_hash, sizeof (hash)) == 0;
}
int
fscan_passwd_entry (FILE *stream, struct passwd_entry *entry)
{
*entry = (struct passwd_entry) {0};
int c;
do c = getc (stream);
while (c == '\n');
if (c == EOF)
return 0;
ungetc (c, stream);
char buffer[256 + 1];
fscanf (stream, "%32[a-zA-Z0-9]", buffer);
if (getc (stream) != ':') return -1;
entry->login = string_view_strdup (buffer);
fread (buffer, 1, PASSWORD_ENCODED_LENGTH, stream);
if (getc (stream) != ':') return -1;
buffer[PASSWORD_ENCODED_LENGTH] = '\0';
entry->encoded_password = string_view_strdup (buffer);
fscanf (stream, "%d", &entry->id);
if (getc (stream) != ':') return -1;
fscanf (stream, "%d", (int *) &entry->access_rights);
if (entry->access_rights > 0b111) return -1;
if (getc (stream) != ':') return -1;
int len = 0;
fscanf (stream, "%256[^:\n]%n", buffer, &len);
if (len)
entry->comment = SVDUP0 ((struct string_view) { buffer, buffer + len });
return 1;
}
void
fprint_passwd_entry (FILE *stream, const struct passwd_entry *entry)
{
if (entry->id < 0)
DIE ("id < 0");
struct string_view comment;
if (entry->comment.begin) {
comment.begin = entry->comment.begin;
comment.end = entry->comment.end;
} else {
comment.begin = "";
comment.end = comment.begin;
}
fprintf (stream, "%.*s:%.*s:%d:%d:%.*s\n",
SVFMT (entry->login),
SVFMT (entry->encoded_password),
entry->id,
entry->access_rights,
SVFMT (comment));
}
void
lock_passwd_file (struct passwd passwd)
{
if (lockf (fileno (passwd.stream), F_TLOCK, 0) != 0)
DIE ("unable to lock passwd file: %s", strerror (errno));
}
void
unlock_passwd_file (struct passwd passwd)
{
if (lockf (fileno (passwd.stream), F_ULOCK, 0) != 0)
DIE ("unable to unlock passwd file: %s", strerror (errno));
}
struct passwd
load_passwd_file (void)
{
elevate ();
struct passwd passwd = {
.stream = fopen (PREFIX"/etc/passwd", "r+"),
};
drop ();
if (passwd.stream == NULL)
DIE ("unable to open passwd file: %s", strerror (errno));
if (lockf (fileno (passwd.stream), F_TEST, 0) != 0)
DIE ("passwd file is locked: %s", strerror (errno));
for (int i = 0;; ++i) {
struct passwd_entry entry;
int ret = fscan_passwd_entry (passwd.stream, &entry);
if (ret == -1)
DIE (PREFIX"/etc/passwd:%d: failed to parse entry", i);
if (ret == 0)
break;
DYNAMIC_ARRAY_APPEND (passwd, entry);
}
return passwd;
}
void
save_passwd_file (struct passwd passwd)
{
ftruncate (fileno (passwd.stream), 0);
rewind (passwd.stream);
for (struct passwd_entry *entry = passwd.items;
entry < passwd.items + passwd.count; ++entry) {
fprint_passwd_entry (passwd.stream, entry);
} fflush (passwd.stream);
}
void
close_passwd_file (struct passwd passwd)
{
for (uint32_t i = 0; i < passwd.count; ++i) {
free ((void *) passwd.items[i].login.begin);
free ((void *) passwd.items[i].encoded_password.begin);
free ((void *) passwd.items[i].comment.begin);
}
DYNAMIC_ARRAY_FREE (passwd);
if (lockf (fileno (passwd.stream), F_ULOCK, 0) != 0)
DIE ("unable to unlock passwd file: %s", strerror (errno));
fclose (passwd.stream);
}
struct string_view
read_password ()
{
static char password[PASSWORD_MAX_LENGTH + 2];
struct termios term_orig;
tcgetattr (fileno (stdin), &term_orig);
struct termios term_noecho = term_orig;
term_noecho.c_lflag &= ~ECHO;
tcsetattr (fileno (stdin), 0, &term_noecho);
if (fgets (password, sizeof (password), stdin) == NULL) {
tcsetattr (fileno (stdin), 0, &term_orig);
putchar ('\n');
exit (0);
} tcsetattr (fileno (stdin), 0, &term_orig);
putchar ('\n');
size_t len = strlen (password) - 1;
if (password[len] != '\n')
DIE ("password too long");
password[len] = '\0';
return (struct string_view) { password, password + len };
}
struct string_view
read_login ()
{
static char login[LOGIN_MAX_LENGTH + 2];
if (fgets (login, sizeof (login), stdin) == NULL) {
putchar ('\n');
exit (0);
}
size_t len = strlen (login) - 1;
if (login[len] != '\n')
DIE ("login too long");
login[len] = '\0';
return (struct string_view) { login, login + len };
}
struct passwd_entry *
authenticate (struct passwd passwd,
struct string_view login,
struct string_view password)
{
struct passwd_entry *entry = lookup_login (passwd, login);
if (!entry) {
fprintf (stderr, "user %.*s not found\n", SVFMT (login));
return NULL;
}
if (!check_password (password, entry->encoded_password)) {
fputs ("authentication failure\n", stderr);
string_builder_printf (&passwd_log, "%u user %.*s supplied incorrect password\n",
time (NULL), SVFMT (entry->login));
return NULL;
}
string_builder_printf (&passwd_log, "%u authenticated user %.*s %d\n",
time (NULL), SVFMT (entry->login), entry->id);
return entry;
}
void
append_passwd_log ()
{
if (passwd_log.count) {
elevate ();
int fd = open (PREFIX"/log/passwd.log", O_CREAT | O_WRONLY | O_APPEND);
drop ();
write (fd, passwd_log.items, passwd_log.count);
close (fd);
DYNAMIC_ARRAY_RESET (passwd_log);
}
}
struct passwd_entry *
lookup_login (struct passwd passwd, struct string_view login)
{
for (struct passwd_entry *entry = passwd.items;
entry < passwd.items + passwd.count; ++entry) {
if (SVCMP (login, entry->login) != 0)
continue;
string_builder_printf (&passwd_log, "%u user '%.*s' (%d) was looked up\n",
time (NULL), SVFMT (entry->login), entry->id);
return entry;
}
string_builder_printf (&passwd_log, "%u user '%.*s' lookup failed\n",
time (NULL), SVFMT (login));
return NULL;
}
struct passwd_entry *
lookup_id (struct passwd passwd, int id)
{
for (struct passwd_entry *entry = passwd.items;
entry < passwd.items + passwd.count; ++entry) {
if (id != entry->id)
continue;
string_builder_printf (&passwd_log, "%u user %d was looked up\n",
time (NULL), id);
return entry;
}
string_builder_printf (&passwd_log, "%u user %d lookup failed\n",
time (NULL), id);
return NULL;
}
void
elevate (void)
{
if (euid != ruid) seteuid (euid);
if (egid != rgid) setegid (egid);
}
void
drop (void)
{
if (euid != ruid) seteuid (ruid);
if (egid != rgid) setegid (rgid);
}
|