projects/pwm

view pwm.c @ 20:efef93e54c5f

Automatically save the database when receiving a fatal signal
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Wed Sep 06 13:56:11 2017 +0200 (2017-09-06)
parents 5c6155c8e9b6
children ee4d36c85287
line source
1 /*
2 * Copyright (C) 2017 Guido Berhoerster <guido+pwm@berhoerster.name>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
24 #include "compat.h"
26 #ifdef HAVE_ERR_H
27 #include <err.h>
28 #endif /* HAVE_ERR_H */
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <langinfo.h>
32 #include <locale.h>
33 #include <pwd.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <pwd.h>
38 #include <sys/stat.h>
39 #include <time.h>
40 #include <unistd.h>
42 #include "pwm.h"
43 #include "cmd.h"
44 #include "io.h"
45 #include "pwfile.h"
46 #include "tok.h"
47 #include "util.h"
49 static void
50 usage(void)
51 {
52 fprintf(stderr, "usage: %s [-P file] [filename]\n", getprogname());
53 }
55 void
56 pwm_err(struct pwm_ctx *ctx, char *fmt, ...)
57 {
58 va_list args;
60 free(ctx->errmsg);
62 if (fmt != NULL) {
63 va_start(args, fmt);
64 xvasprintf(&ctx->errmsg, fmt, args);
65 va_end(args);
67 fprintf(stderr, "%s\n", ctx->errmsg);
68 } else {
69 ctx->errmsg = NULL;
70 }
71 }
73 void
74 pwm_block_signals(void)
75 {
76 sigset_t set;
78 sigemptyset(&set);
79 sigaddset(&set, SIGINT);
80 sigaddset(&set, SIGTERM);
81 sigaddset(&set, SIGHUP);
82 sigaddset(&set, SIGQUIT);
83 if (sigprocmask(SIG_BLOCK, &set, NULL) != 0) {
84 err(1, "sigprocmask");
85 }
86 }
88 void
89 pwm_unblock_signals(void)
90 {
91 sigset_t set;
93 sigemptyset(&set);
94 sigaddset(&set, SIGINT);
95 sigaddset(&set, SIGTERM);
96 sigaddset(&set, SIGHUP);
97 sigaddset(&set, SIGQUIT);
98 if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) {
99 err(1, "sigprocmask");
100 }
101 }
103 static int
104 run_input_loop(struct pwm_ctx *ctx, int is_interactive)
105 {
106 int retval = -1;
107 char prompt[8 + 2 + 1];
108 GetLine *gl = NULL;
109 char buf[PWM_LINE_MAX + 1];
110 int io_retval;
111 int argc = 0;
112 char **argv = NULL;
113 struct cmd *cmd;
114 int i;
116 snprintf(prompt, sizeof (prompt), "%.*s> ", 8, getprogname());
118 pwm_block_signals();
120 /* initialize libtecla */
121 gl = new_GetLine(PWM_LINE_MAX, PWM_HISTORY_MAX);
122 if (gl == NULL) {
123 err(1, "new_GetLine");
124 }
125 gl_catch_blocked(gl);
126 gl_limit_history(gl, PWM_HISTORY_LINES_MAX);
127 /* disable default filename completion */
128 gl_customize_completion(gl, NULL, io_gl_complete_nothing);
130 for (;;) {
131 /* read next line */
132 cmd = NULL;
133 buf[0] = '\0';
134 io_retval = io_get_line(gl, prompt, 1, NULL, 0,
135 sizeof (buf), buf);
136 switch (io_retval) {
137 case IO_OK:
138 break;
139 case IO_TRUNCATED:
140 /* line was truncated in non-interactive mode */
141 fprintf(stderr, "line too long\n");
142 goto out;
143 case IO_EOF: /* FALLTHROUGH */
144 case IO_SIGNAL:
145 if (ctx->unsaved_changes) {
146 pwfile_write_autosave_file(ctx);
147 }
148 goto quit;
149 default:
150 fprintf(stderr, "unknown error\n");
151 goto quit;
152 }
154 /* tokenize line */
155 switch (tok_tokenize(buf, &argc, &argv)) {
156 case TOK_ERR_SYSTEM_ERROR:
157 err(1, "tok_tokenize");
158 case TOK_ERR_UNTERMINATED_QUOTE:
159 fprintf(stderr, "unterminated quote\n");
160 if (!is_interactive) {
161 goto out;
162 }
163 goto next;
164 case TOK_ERR_TRAILING_BACKSLASH:
165 fprintf(stderr, "trailing backslash\n");
166 if (!is_interactive) {
167 goto out;
168 }
169 goto next;
170 }
172 if (argc == 0) {
173 /* empty line */
174 goto next;
175 }
177 /* find and execute the command */
178 cmd = cmd_match(argv[0]);
179 if (cmd == NULL) {
180 pwm_err(ctx, "unknown command: %s", argv[0]);
181 if (is_interactive) {
182 goto next;
183 } else {
184 goto out;
185 }
186 }
187 switch (cmd->cmd_func(ctx, argc, argv)) {
188 case CMD_OK:
189 pwm_err(ctx, NULL); /* clear error */
190 break;
191 case CMD_USAGE:
192 fprintf(stderr, "usage: %s\n", cmd->usage);
193 case CMD_ERR: /* FALLTHROUGH */
194 if (!is_interactive) {
195 goto out;
196 }
197 break;
198 case CMD_SIGNAL:
199 fprintf(stderr, "received signal, quitting\n");
200 case CMD_QUIT: /* FALLTHROUGH */
201 goto quit;
202 }
203 ctx->prev_cmd = cmd->full_cmd;
205 next:
206 for (i = 0; i < argc; i++) {
207 free(argv[i]);
208 }
209 free(argv);
210 argc = 0;
211 argv = NULL;
212 }
214 quit:
215 retval = 0;
217 out:
218 for (i = 0; i < argc; i++) {
219 free(argv[i]);
220 }
221 free(argv);
222 del_GetLine(gl);
224 return (retval);
225 }
227 int
228 pwm_read_password(struct pwm_ctx *ctx, int is_new_password)
229 {
230 switch (io_get_password(is_new_password ? "New Password:" :
231 "Password:", is_new_password ? "Confirm Password:" : NULL,
232 sizeof (ctx->password), ctx->password)) {
233 case IO_OK:
234 return (0);
235 case IO_SIGNAL:
236 return (-2);
237 case IO_PASSWORD_EMPTY:
238 pwm_err(ctx, "password must not be empty");
239 return (-1);
240 case IO_PASSWORD_MISMATCH:
241 pwm_err(ctx, "passwords do not match");
242 return (-1);
243 default:
244 return (-1);
245 }
246 }
248 static int
249 read_password_from_file(const char *filename, char *password,
250 size_t password_size)
251 {
252 int retval = -1;
253 char *buf = NULL;
254 FILE *fp = NULL;
255 size_t password_len = 0;
257 buf = xmalloc(password_size);
259 fp = fopen(filename, "r");
260 if (fp == NULL) {
261 warn("failed to open master password file \"%s\"", filename);
262 goto out;
263 }
264 if (fgets(buf, password_size, fp) == NULL) {
265 /* read error or empty file */
266 if (ferror(fp)) {
267 /* read error */
268 warn("failed to read master password from \"%s\"",
269 filename);
270 }
271 goto out;
272 }
273 password_len = strlen(buf);
274 if (buf[password_len - 1] == '\n') {
275 /* strip trailing newline */
276 password_len--;
277 if (password_len == 0) {
278 /* first line is empty */
279 fprintf(stderr, "malformed password file\n");
280 goto out;
281 }
282 } else if (!feof(fp)) {
283 /* the first line was truncated, password is too long */
284 fprintf(stderr, "malformed password file\n");
285 goto out;
286 }
287 memcpy(password, buf, password_size);
288 retval = 0;
290 out:
291 password[password_len] = '\0';
293 if (fp != NULL) {
294 fclose(fp);
295 }
296 free(buf);
298 return (retval);
299 }
301 int
302 main(int argc, char *argv[])
303 {
304 int status = EXIT_FAILURE;
305 char *locale;
306 int is_interactive;
307 int errflag = 0;
308 int c;
309 const char *master_password_filename = NULL;
310 struct pwm_ctx ctx = { 0 };
311 struct passwd *passwd;
312 FILE *fp = NULL;
314 setprogname(argv[0]);
316 /* set up locale and check for UTF-8 codeset */
317 locale = setlocale(LC_ALL, "");
318 if (locale == NULL) {
319 errx(1, "invalid locale");
320 }
321 if ((strcasecmp(nl_langinfo(CODESET), "UTF-8") != 0) &&
322 (strcasecmp(nl_langinfo(CODESET), "UTF8") != 0)) {
323 fprintf(stderr, "pwm requires a locale with UTF-8 character "
324 "encoding.\n");
325 goto out;
326 }
328 /* timestamps are processed as UTC */
329 if (setenv("TZ", "", 1) != 0) {
330 goto out;
331 }
332 tzset();
334 /* initialize libpws */
335 if (pws_init() != 0) {
336 goto out;
337 }
339 is_interactive = isatty(STDIN_FILENO);
341 while (!errflag && (c = getopt(argc, argv, "P:h")) != -1) {
342 switch (c) {
343 case 'P':
344 master_password_filename = optarg;
345 break;
346 case 'h':
347 usage();
348 status = EXIT_SUCCESS;
349 goto out;
350 default:
351 errflag = 1;
352 }
353 }
354 if (errflag || (optind + 1 < argc)) {
355 usage();
356 status = EXIT_USAGE;
357 goto out;
358 }
360 passwd = getpwuid(getuid());
361 if (passwd == NULL) {
362 err(1, "getpwuid");
363 }
364 xasprintf(&ctx.dirname, "%s/.pwm", passwd->pw_dir);
366 /* create ~/.pwm directory if necessary */
367 if ((mkdir(ctx.dirname, S_IRWXU) != 0) && (errno != EEXIST)) {
368 warn("failed to create directory \"%s\"", ctx.dirname);
369 goto out;
370 }
372 if (optind == argc) {
373 xasprintf(&ctx.filename, "%s/pwm.psafe3", ctx.dirname);
374 } else if (optind + 1 == argc) {
375 ctx.filename = xstrdup(argv[optind++]);
376 } else {
377 usage();
378 status = EXIT_USAGE;
379 goto out;
380 }
382 if (is_interactive) {
383 printf("pwm version %s\n", VERSION);
384 } else if (master_password_filename == NULL) {
385 fprintf(stderr, "master password file must be specified when "
386 "running non-interactively\n");
387 goto out;
388 }
390 pwfile_init(&ctx);
392 fp = fopen(ctx.filename, "r");
393 if ((fp == NULL) && (errno != ENOENT)) {
394 warn("failed to open password database");
395 goto out;
396 }
397 /* obtain master password */
398 if (master_password_filename != NULL) {
399 if (read_password_from_file(master_password_filename,
400 ctx.password, sizeof (ctx.password)) != 0) {
401 goto out;
402 }
403 } else if (pwm_read_password(&ctx, (fp == NULL)) != 0) {
404 goto out;
405 }
406 if (fp != NULL) {
407 if (pwfile_read_file(&ctx, fp) != 0) {
408 goto out;
409 }
410 fclose(fp);
411 fp = NULL;
412 }
414 /* run main input loop */
415 status = (run_input_loop(&ctx, is_interactive) != 0);
417 out:
418 pwfile_destroy(&ctx);
419 if (fp != NULL) {
420 fclose(fp);
421 }
422 free(ctx.filename);
423 free(ctx.dirname);
424 free(ctx.errmsg);
426 exit(status);
427 }