-
Notifications
You must be signed in to change notification settings - Fork 1
/
jrnlc.c
334 lines (277 loc) · 7.79 KB
/
jrnlc.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
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
/*
* Copyright (c) 2021 Matthias Schmidt <xhr@giessen.ccc.de>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/stat.h>
#include <sys/types.h>
#include <limits.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#ifdef __OpenBSD__
#include <readpassphrase.h>
#endif
#include "jrnlc.h"
static int debug = 0;
static int backup = 0;
static struct config conf;
static char jrnlc_config[_POSIX_PATH_MAX];
static unsigned char nonce[crypto_secretbox_NONCEBYTES];
extern char *__progname;
static void
usage(void)
{
log_fatal(1, "%s [-aBde] [-DIn number]\n", __progname);
}
int
main(int argc, char **argv)
{
char *passbuf;
int encrypt = 0;
int decrypt = 0;
int last = 0;
int entry = 0;
int to_delete = 0;
int ch;
conf.jrnlc_journal[0] = '\0';
while ((ch = getopt(argc, argv, "aBdD:ef:I:n:vV")) != -1) {
switch (ch) {
case 'a':
last = -1;
break;
case 'B':
backup = 1;
break;
case 'd':
decrypt = 1;
break;
case 'D':
to_delete = get_number(optarg);
break;
case 'e':
encrypt = 1;
break;
case 'I':
entry = get_number(optarg);
break;
case 'n':
last = get_number(optarg);
break;
case 'v':
debug++;
break;
case 'V':
printf("Version %s\n", VERSION);
exit(0);
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (decrypt && encrypt) {
log_fatal(1, "Please specify either -d or -e, but not both\n");
}
if (entry > 0 && last > 0) {
log_fatal(1, "Please specify either -I or -n, but not both\n");
}
/* Check if base directory exists, if not create it */
setup_base_dir();
sandbox();
/* Load existing config file or create a new one with default values */
(void)load_config();
if (sodium_init() == -1)
log_fatal(1, "Cannot initialize sodium crypto library\n");
passbuf = calloc_wrapper(1, MAX_PASS_LEN);
/* User supplied either -e or -d */
if (encrypt && conf.encrypted) {
log_fatal(1, "Journal is already encrypted\n");
} else if (decrypt && !conf.encrypted) {
log_fatal(1, "Journal is already decrypted\n");
} else if (encrypt && !conf.encrypted) {
/* User wants to encrypt and journal is unencrypted */
printf("Encrypting journal...\n");
int res = get_initial_passphrase(passbuf);
if (res == 2) {
sodium_memzero(passbuf, sizeof(passbuf));
log_fatal(1, "No blank password allowed\n");
} else if (res == 3) {
sodium_memzero(passbuf, sizeof(passbuf));
log_fatal(1, "Passwords do not match\n");
}
/* Generate a new random key and nonce ... */
crypto_secretbox_keygen(conf.key);
randombytes_buf(nonce, sizeof nonce);
/* ... and save both in the key pass file */
write_pass_file((unsigned char *)conf.key, passbuf);
sodium_memzero(passbuf, sizeof(passbuf));
free(passbuf);
passbuf = NULL;
load_journal_from_disk();
/* Make journal as encrypted and save it to disk */
conf.encrypted = 1;
shutdown(0);
} else if (decrypt && conf.encrypted) {
/* User wants to decrypt and journal is encrypted */
printf("Decrypting journal...\n");
read_password("Enter Password: ", passbuf);
/* Load the nonce first, otherwise decryption of the key fails */
load_nonce(nonce);
/* Load key from disk, encrypt it and save it to memory */
memcpy(conf.key, load_pass_file(passbuf), crypto_secretbox_KEYBYTES);
sodium_memzero(passbuf, sizeof(passbuf));
free(passbuf);
passbuf = NULL;
load_journal_from_disk();
/* Make journal as decrypted and save it to disk */
conf.encrypted = 0;
shutdown(0);
}
if (is_encrypted()) {
read_password("Enter Password: ", passbuf);
/* Load the nonce first, otherwise decryption of the key fails */
load_nonce(nonce);
/* Load key from disk, encrypt it and save it to memory */
memcpy(conf.key, load_pass_file(passbuf), crypto_secretbox_KEYBYTES);
sodium_memzero(passbuf, sizeof(passbuf));
free(passbuf);
passbuf = NULL;
}
/* Load the journal file into memory */
load_journal_from_disk();
if (to_delete > 0) {
if (!delete_entry(to_delete)) {
log_fatal(1, "Entry %d not found. Nothing to delete\n", to_delete);
}
shutdown(0);
} else if (entry > 0) {
print_single_journal_entry(entry);
} else if (last < 0) {
/* Show all entries */
print_journal_entries(-1);
} else if (last > 0) {
print_journal_entries(last);
} else {
create_new_journal_entry();
}
/* Free passbuf in case of an unencrypted journal */
free(passbuf);
passbuf = NULL;
shutdown(0);
return 0;
}
void
shutdown(int prio)
{
/* User requested a plain text backup with -B */
if (backup)
save_journal_to_disk(NULL, 1);
save_journal_to_disk(get_jrnlc_journal(), 0);
write_config();
sodium_memzero(conf.key, crypto_secretbox_KEYBYTES);
exit(prio);
}
void
setup_base_dir(void)
{
struct stat sb;
char *home, *xdg_home;
int ret;
if ((xdg_home = getenv("XDG_CONFIG_HOME")) != NULL) {
ret = snprintf(conf.jrnlc_dir, sizeof(conf.jrnlc_dir), "%s/jrnlc", xdg_home);
if (ret < 0 || (size_t)ret >= sizeof(conf.jrnlc_dir)) {
log_fatal(1, "Path truncation happened. Buffer to short to fit %s\n", conf.jrnlc_dir);
}
} else if ((home = getenv("HOME")) != NULL) {
ret = snprintf(conf.jrnlc_dir, sizeof(conf.jrnlc_dir), "%s/.config/jrnlc", home);
if (ret < 0 || (size_t)ret >= sizeof(conf.jrnlc_dir)) {
log_fatal(1, "Path truncation happened. Buffer to short to fit %s\n", conf.jrnlc_dir);
}
} else {
log_fatal(1, "Neither $XDG_CONFIG_HOME nor $HOME is set!\n");
}
if (stat(conf.jrnlc_dir, &sb) == 0 && S_ISDIR(sb.st_mode)) {
log_debug(1, "%s already exists\n", conf.jrnlc_dir);
} else {
log_debug(1, "%s does not exists. Attempt to create it\n", conf.jrnlc_dir);
if (mkdir(conf.jrnlc_dir, 0700) == -1) {
log_fatal(1, "Cannot create %s directory\n", conf.jrnlc_dir);
}
}
/* Set up path to the journal JSON file */
ret = snprintf(conf.jrnlc_journal, _POSIX_PATH_MAX, "%s/journal.json", conf.jrnlc_dir);
if (ret < 0 || (size_t)ret >= sizeof(conf.jrnlc_journal)) {
log_fatal(1, "Path truncation happened. Buffer to short to fit %s\n", conf.jrnlc_dir);
}
/* Set up path for config JSON file */
ret = snprintf(jrnlc_config, _POSIX_PATH_MAX, "%s/config.json", conf.jrnlc_dir);
if (ret < 0 || (size_t)ret >= sizeof(jrnlc_config)) {
log_fatal(1, "Path truncation happened. Buffer to short to fit %s\n", conf.jrnlc_dir);
}
/* Set up path for key JSON file */
ret = snprintf(conf.key_path, _POSIX_PATH_MAX, "%s/key.json", conf.jrnlc_dir);
if (ret < 0 || (size_t)ret >= sizeof(conf.key_path)) {
log_fatal(1, "Path truncation happened. Buffer to short to fit %s\n", conf.jrnlc_dir);
}
}
const unsigned char *
get_nonce(void)
{
return nonce;
}
const char*
get_jrnlc_journal(void)
{
return conf.jrnlc_journal;
}
int
is_encrypted(void)
{
return conf.encrypted;
}
struct config *
get_config(void)
{
return &conf;
}
const char*
get_config_path(void)
{
return jrnlc_config;
}
void
log_debug(int prio, const char *fmt, ...)
{
va_list ap;
if (debug == 0)
return;
if (debug < prio)
return;
va_start(ap, fmt);
fprintf(stdout, "[*] ");
vfprintf(stdout, fmt, ap);
va_end(ap);
}
void
log_fatal(int prio, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
exit(prio);
}