changeset 29:00d782cb45fa

Read configuration file on startup
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Thu, 28 Sep 2017 10:06:59 +0200
parents e3db02d7f1f4
children 2552eec9b913
files Makefile pwm.1.xml pwm.c pwmrc.c pwmrc.h
diffstat 5 files changed, 245 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- 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
--- 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 @@
   </refsect1>
   <refsect1>
     <title>Usage</title>
-    <para>If stdin is connected to a terminal pwm will run in interactive mode
-    and prompt the user for the master password unless
-    <replaceable>password_file</replaceable> is specified via the
-    <option>-P</option> option. After successfully opening the password
-    database the user will be prompted for a command.</para>
-    <para>When running in non-interactive mode a file containing the master
-    pasword must be specified via the <option>-P</option> 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.</para>
-    <para><command>pwm</command> operates on a copy of the password database
-    in memory, any changes must be explicitly written back to the database
-    using the write command.</para>
+    <refsect2>
+      <title>Start-up</title>
+      <para>If stdin is connected to a terminal pwm will run in interactive mode
+      and prompt the user for the master password unless
+      <replaceable>password_file</replaceable> is specified via the
+      <option>-P</option> option. After successfully opening the password
+      database the user will be prompted for a command.</para>
+      <para>When running in non-interactive mode a file containing the master
+      pasword must be specified via the <option>-P</option> 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.</para>
+      <para><command>pwm</command> operates on a copy of the password database
+      in memory, any changes must be explicitly written back to the database
+      using the write command.</para>
+      <para>When starting up, before prompting the user for a master password
+      or reading the master password from a specified file
+      <command>pwm</command> will read the file
+      <filename>~/.pwm/pwmrc</filename> and execute any <command>set</command>
+      and <command>define</command> commands specified therein.</para>
+    </refsect2>
     <refsect2>
       <title>Configuration Variables</title>
       <para><command>pwm</command> can be configured through configuration
@@ -732,6 +741,12 @@
     <title>Files</title>
     <variablelist>
       <varlistentry>
+        <term><filename>~/.pwm/pwmrc</filename></term>
+        <listitem>
+          <para>configuration file</para>
+        </listitem>
+      </varlistentry>
+      <varlistentry>
         <term><filename>~/.pwm/pwm.psafe3</filename></term>
         <listitem>
           <para>default password database</para>
--- 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");
--- /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 <guido+pwm@berhoerster.name>
+ *
+ * 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 <errno.h>
+#include <fcntl.h>
+#ifdef	HAVE_ERR_H
+#include <err.h>
+#endif /* HAVE_ERR_H */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#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);
+}
--- /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 <guido+pwm@berhoerster.name>
+ *
+ * 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 */