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
|
/* See LICENSE file for copyright and license details. */
#include <sys/auxv.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "arg.h"
char *argv0;
static void
usage(void)
{
fprintf(stderr, "usage: %s [-r] user key-name [crypt-parameters]\n", argv0);
exit(1);
}
static int
writeall(int fd, const char *data, size_t len)
{
size_t off = 0;
ssize_t r;
while (off < len) {
r = write(fd, &data[off], len - off);
if (r < 0)
return -1;
off += (size_t)r;
}
return 0;
}
static int
checkkey(char *data, size_t whead, size_t *rheadp, size_t *rhead2p, size_t *linenop,
const char *keyname, size_t klen, const char *path)
{
int failed = 0;
size_t len;
while (*rhead2p < whead || data[*rhead2p] != '\n')
++*rhead2p;
if (data[*rhead2p] != '\n')
return 0;
len = *rhead2p - *rheadp;
*linenop += 1;
if (memchr(&data[*rheadp], '\0', len)) {
fprintf(stderr, "%s: NUL byte found in %s on line %zu\n", argv0, path, *linenop);
failed = 1;
}
if (!memchr(&data[*rheadp], ' ', len)) {
fprintf(stderr, "%s: no SP byte found in %s on line %zu\n", argv0, path, *linenop);
failed = 1;
}
if (failed || klen >= len || data[*rheadp + klen] != ' ' || memcpy(&data[*rheadp], keyname, klen)) {
*rheadp = ++*rhead2p;
return 0;
} else {
++*rhead2p;
return 1;
}
}
static void
loadandlocate(size_t *beginning_out, size_t *end_out, int fd, char **datap, size_t *lenp, size_t *sizep,
const char *keyname, const char *path)
{
size_t klen = strlen(keyname);
char *new;
size_t rhead = 0;
size_t rhead2 = 0;
size_t lineno = 0;
ssize_t r = 1;
while (r) {
if (*lenp == *sizep) {
new = realloc(*datap, *sizep += 4096);
if (!new) {
fprintf(stderr, "%s: realloc: %s\n", argv0, strerror(errno));
exit(1);
}
*datap = new;
}
r = read(fd, &(*datap)[*lenp], *sizep - *lenp);
if (r < 0) {
fprintf(stderr, "%s: read %s: %s\n", argv0, path, strerror(errno));
exit(1);
}
*lenp += (size_t)r;
while (rhead2 < *lenp) {
if (!checkkey(*datap, *lenp, &rhead, &rhead2, &lineno, keyname, klen, path))
continue;
*beginning_out = rhead;
*end_out = rhead = rhead2;
}
}
if (rhead != *lenp) {
fprintf(stderr, "%s: file truncated: %s\n", argv0, path);
if (memchr(&(*datap)[rhead], '\0', *lenp - rhead))
fprintf(stderr, "%s: NUL byte found in %s on line %zu\n", argv0, path, lineno + 1);
}
}
static char *
mksalt(char *p)
{
uintptr_t rdata_addr;
char *rdata;
size_t i;
rdata_addr = (uintptr_t)getauxval(AT_RANDOM); /* address to 16 random bytes */
rdata = (void *)rdata_addr;
if (!rdata) {
fprintf(stderr, "%s: getauxval AT_RANDOM: %s\n", argv0, strerror(errno));
exit(1);
}
for (i = 0; i < 16; i++)
*p++ = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789./"[*rdata++ & 63];
return p;
}
int
main(int argc, char *argv[])
{
const char *user;
const char *keyname;
const char *parameters;
char *path, *path2;
char *data = NULL;
size_t data_len = 0;
size_t data_size = 0;
size_t beginning = 0;
size_t end = 0;
int allow_replace = 0;
int failed = 0;
int fd;
char *key = NULL, *new;
size_t key_len = 0;
size_t key_size = 0;
char *hash;
size_t gap_size;
size_t gap_increase;
#define HASH_PREFIX "$6$"
char generated_parameters[sizeof(HASH_PREFIX"$") + 16];
ssize_t r;
ARGBEGIN {
case 'r':
allow_replace = 1;
break;
default:
usage();
} ARGEND;
if (argc < 2 || argc > 3)
usage();
user = argv[0];
keyname = argv[1];
parameters = argv[2];
if (!user[0] || user[0] == '.' || strchr(user, '/') || strchr(user, '~')) {
fprintf(stderr, "%s: bad user name specified: %s\n", argv0, user);
failed = 1;
}
if (keyname[strcspn(keyname, " \t\f\n\r\v")]) {
fprintf(stderr, "%s: bad key name specified: %s, includes whitespace\n", argv0, keyname);
failed = 1;
}
if (isatty(STDIN_FILENO)) {
fprintf(stderr, "%s: standard input must not be a TTY.\n", argv0);
failed = 1;
}
if (failed)
return 1;
if (mlockall(MCL_CURRENT | MCL_FUTURE)) {
fprintf(stderr, "%s: mlockall MCL_CURRENT|MCL_FUTURE: %s\n", argv0, strerror(errno));
exit(1);
}
if (!parameters) {
stpcpy(mksalt(stpcpy(generated_parameters, HASH_PREFIX)), "$");
parameters = generated_parameters;
}
for (;;) {
if (key_len == key_size) {
new = malloc(1 + (key_size += 1024));
if (!new) {
explicit_bzero(key, key_len);
fprintf(stderr, "%s: read <stdin>: %s\n", argv0, strerror(errno));
exit(1);
}
memcpy(new, key, key_len);
explicit_bzero(key, key_len);
free(key);
key = new;
}
r = read(STDIN_FILENO, &key[key_len], key_size - key_len);
if (r <= 0) {
if (!r)
break;
explicit_bzero(key, key_len);
fprintf(stderr, "%s: read <stdin>: %s\n", argv0, strerror(errno));
exit(1);
}
key_len += (size_t)r;
}
key[key_len] = '\0';
hash = crypt(key, parameters);
if (!hash)
fprintf(stderr, "%s: crypt <key> %s: %s\n", argv0, parameters, strerror(errno));
explicit_bzero(key, key_len);
free(key);
key_size = key_len = strlen(keyname) + strlen(hash) + 3;
key = malloc(key_len);
if (!key) {
fprintf(stderr, "%s: malloc: %s\n", argv0, strerror(errno));
exit(1);
}
stpcpy(stpcpy(stpcpy(stpcpy(key, keyname), " "), hash), "\n");
path = malloc(sizeof(KEYPATH"/") + strlen(user));
if (!path) {
fprintf(stderr, "%s: malloc: %s\n", argv0, strerror(errno));
exit(1);
}
path2 = malloc(sizeof(KEYPATH"/~") + strlen(user));
if (!path) {
fprintf(stderr, "%s: malloc: %s\n", argv0, strerror(errno));
exit(1);
}
stpcpy(stpcpy(path, KEYPATH"/"), user);
stpcpy(stpcpy(path2, path), "~");
fd = open(path, O_RDONLY);
if (fd < 0) {
if (errno != ENOENT) {
fprintf(stderr, "%s: open %s O_RDONLY: %s\n", argv0, path, strerror(errno));
exit(1);
}
beginning = end = 0;
} else {
loadandlocate(&beginning, &end, fd, &data, &data_len, &data_size, keyname, path);
if (close(fd)) {
fprintf(stderr, "%s: read %s: %s\n", argv0, path, strerror(errno));
exit(1);
}
if (beginning == end)
beginning = end = 0; /* make sure the new key is not concatenated onto a truncated line at the end */
}
if (!allow_replace && beginning != end) {
fprintf(stderr, "%s: key already exists: %s\n", argv0, keyname);
exit(1);
}
if (end < beginning)
abort();
gap_size = end - beginning;
if (gap_size > key_len) {
memmove(&data[beginning + key_len], &data[end], data_len - end);
data_len -= key_len - gap_size;
} else if (gap_size < key_len) {
gap_increase = key_len - gap_size;
if (data_len + gap_increase > data_size) {
data_size = data_len + gap_increase;
data = realloc(data, data_size);
if (!data) {
fprintf(stderr, "%s: realloc: %s\n", argv0, strerror(errno));
exit(1);
}
}
memmove(&data[end], &data[end + gap_increase], data_len - end);
data_len += gap_increase;
}
memcpy(&data[beginning], key, key_len);
free(key);
if (mkdir(KEYPATH, 0700) && errno != EEXIST) {
fprintf(stderr, "%s: mkdir %s: %s\n", argv0, KEYPATH, strerror(errno));
exit(1);
}
fd = open(path2, O_WRONLY | O_CREAT | O_EXCL, 0600);
if (fd < 0) {
fprintf(stderr, "%s: open %s O_WRONLY|O_CREAT|O_EXCL 0600: %s\n", argv0, path2, strerror(errno));
exit(1);
}
if (writeall(fd, data, data_len)) {
fprintf(stderr, "%s: write %s: %s\n", argv0, path2, strerror(errno));
close(fd);
goto saved_failed;
}
if (close(fd)) {
fprintf(stderr, "%s: write %s: %s\n", argv0, path2, strerror(errno));
goto saved_failed;
}
if (rename(path2, path)) {
fprintf(stderr, "%s: rename %s %s: %s\n", argv0, path2, path, strerror(errno));
saved_failed:
if (unlink(path2))
fprintf(stderr, "%s: unlink %s: %s\n", argv0, path2, strerror(errno));
exit(1);
}
free(path);
free(path2);
free(data);
return 0;
}
|