# HG changeset patch # User Guido Berhoerster # Date 1506586019 -7200 # Node ID 00d782cb45facdcc9441086a80b8d4b94f494bc1 # Parent e3db02d7f1f4b7ec75e08341dc291210aa783c2d Read configuration file on startup diff -r e3db02d7f1f4 -r 00d782cb45fa Makefile --- a/Makefile Wed Sep 27 19:44:05 2017 +0200 +++ b/Makefile Thu Sep 28 10:06:59 2017 +0200 @@ -168,6 +168,7 @@ pw.o \ pwfile.o \ pwm.o \ + pwmrc.o \ tok.o PWM_BIN = $(PACKAGE) PWM_MAN1 = pwm.1 diff -r e3db02d7f1f4 -r 00d782cb45fa pwm.1.xml --- a/pwm.1.xml Wed Sep 27 19:44:05 2017 +0200 +++ b/pwm.1.xml Thu Sep 28 10:06:59 2017 +0200 @@ -125,18 +125,27 @@ Usage - If stdin is connected to a terminal pwm will run in interactive mode - and prompt the user for the master password unless - password_file is specified via the - option. After successfully opening the password - database the user will be prompted for a command. - When running in non-interactive mode a file containing the master - pasword must be specified via the option and after - successfully opening the password database, pwm will execute commands read - from stdin until either an error occurrs or end-of-file is reached. - pwm operates on a copy of the password database - in memory, any changes must be explicitly written back to the database - using the write command. + + Start-up + If stdin is connected to a terminal pwm will run in interactive mode + and prompt the user for the master password unless + password_file is specified via the + option. After successfully opening the password + database the user will be prompted for a command. + When running in non-interactive mode a file containing the master + pasword must be specified via the option and after + successfully opening the password database, pwm will execute commands + read from stdin until either an error occurrs or end-of-file is + reached. + pwm operates on a copy of the password database + in memory, any changes must be explicitly written back to the database + using the write command. + When starting up, before prompting the user for a master password + or reading the master password from a specified file + pwm will read the file + ~/.pwm/pwmrc and execute any set + and define commands specified therein. + Configuration Variables pwm can be configured through configuration @@ -732,6 +741,12 @@ Files + ~/.pwm/pwmrc + + configuration file + + + ~/.pwm/pwm.psafe3 default password database diff -r e3db02d7f1f4 -r 00d782cb45fa pwm.c --- a/pwm.c Wed Sep 27 19:44:05 2017 +0200 +++ b/pwm.c Thu Sep 28 10:06:59 2017 +0200 @@ -44,6 +44,7 @@ #include "io.h" #include "macro.h" #include "pwfile.h" +#include "pwmrc.h" #include "tok.h" #include "util.h" @@ -365,6 +366,8 @@ ctx.is_interactive = isatty(STDIN_FILENO); + macro_init(&ctx.macro_head); + while (!errflag && (c = getopt(argc, argv, "P:Rh")) != -1) { switch (c) { case 'P': @@ -417,9 +420,12 @@ goto out; } + /* read ~/.pwm/pwmrc */ + if (pwmrc_read(&ctx) != 0) { + goto out; + } + pwfile_init(&ctx); - macro_init(&ctx.macro_head); - fp = fopen(ctx.filename, "r"); if ((fp == NULL) && (errno != ENOENT)) { warn("failed to open password database"); diff -r e3db02d7f1f4 -r 00d782cb45fa pwmrc.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pwmrc.c Thu Sep 28 10:06:59 2017 +0200 @@ -0,0 +1,180 @@ +/* + * Copyright (C) 2017 Guido Berhoerster + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include "compat.h" + +#include +#include +#ifdef HAVE_ERR_H +#include +#endif /* HAVE_ERR_H */ +#include +#include +#include +#include +#include + +#include "pwm.h" +#include "cmd.h" +#include "io.h" +#include "macro.h" +#include "tok.h" +#include "util.h" + +static const char *config_cmds[] = { + "set", + "define" +}; + +int +pwmrc_read(struct pwm_ctx *ctx) +{ + int retval = -1; + char *filename = NULL; + FILE *fp = NULL; + int lineno; + char buf[PWM_LINE_MAX] = { '\0' }; + size_t tokenc = 0; + union tok **tokenv = NULL; + int argc = 0; + char **argv = NULL; + struct cmd *cmd; + size_t i; + int is_config_cmd; + int j; + + xasprintf(&filename, "%s/pwmrc", ctx->dirname); + + fp = fopen(filename, "r"); + if (fp == NULL) { + if (errno == ENOENT) { + retval = 0; + } else { + warn("failed to open configuration file \"%s\"", + filename); + } + goto out; + } + + for (lineno = 1;; lineno++) { + /* read next line */ + is_config_cmd = 0; + if (fgets(buf, sizeof (buf), fp) == NULL) { + if (feof(fp)) { + goto done; + } + warn("failed to read from configuration file \"%s\"", + filename); + goto out; + } + if (strchr(buf, '\n') == NULL) { + fprintf(stderr, "line %d too long\n", lineno); + goto out; + } + + /* tokenize line */ + switch (tok_tokenize(buf, &tokenc, &tokenv)) { + case TOK_ERR_UNTERMINATED_QUOTE: + fprintf(stderr, "unterminated quote in line %d\n", + lineno); + if (!ctx->is_interactive) { + goto out; + } + goto next; + case TOK_ERR_TRAILING_BACKSLASH: + fprintf(stderr, "trailing backslash in line %d\n", + lineno); + if (!ctx->is_interactive) { + goto out; + } + goto next; + case TOK_ERR_INVALID_MACRO_NAME: + fprintf(stderr, "invalid macro name in line %d\n", + lineno); + if (!ctx->is_interactive) { + goto out; + } + goto next; + } + + /* expand macros */ + if (macro_expand_macros(ctx->macro_head, tokenc, tokenv, &argc, + &argv) != 0) { + fprintf(stderr, "undefined macro in line %d\n", lineno); + if (!ctx->is_interactive) { + goto out; + } + goto next; + } + + if (argc == 0) { + /* empty line */ + goto next; + } + + /* find and execute the command */ + cmd = cmd_match(argv[0]); + for (i = 0; (cmd != NULL) && (i < COUNTOF(config_cmds)); i++) { + if (strcmp(cmd->full_cmd, config_cmds[i]) == 0) { + is_config_cmd = 1; + break; + } + } + if (!is_config_cmd) { + fprintf(stderr, "unknown configuration command \"%s\"" + " in line %d\n", argv[0], lineno); + goto out; + } + if (cmd->cmd_func(ctx, argc, argv) != CMD_OK) { + fprintf(stderr, "syntax error in line %d\n", lineno); + goto out; + } + +next: + for (j = 0; j < argc; j++) { + free(argv[j]); + } + free(argv); + argc = 0; + argv = NULL; + tok_free(tokenv); + tokenc = 0; + tokenv = NULL; + } + +done: + retval = 0; + +out: + for (j = 0; j < argc; j++) { + free(argv[j]); + } + free(argv); + tok_free(tokenv); + if (fp != NULL) { + fclose(fp); + } + free(filename); + + return (retval); +} diff -r e3db02d7f1f4 -r 00d782cb45fa pwmrc.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pwmrc.h Thu Sep 28 10:06:59 2017 +0200 @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2017 Guido Berhoerster + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef PWMRC_H +#define PWMRC_H + +int pwmrc_read(struct pwm_ctx *); + +#endif /* PWMRC_H */