projects/pwm

changeset 23:1b89066d992c

Add read-only mode
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Sun Sep 17 18:45:05 2017 +0200 (2017-09-17)
parents ec01c579024a
children eb5ce870eb16
files cmd.c pwm.1.xml pwm.c pwm.h
line diff
     1.1 --- a/cmd.c	Thu Sep 07 12:40:50 2017 +0200
     1.2 +++ b/cmd.c	Sun Sep 17 18:45:05 2017 +0200
     1.3 @@ -228,9 +228,15 @@
     1.4  			return (CMD_SIGNAL);
     1.5  		}
     1.6  	}
     1.7 -	if (io_printf("There are%sunsaved changes\n",
     1.8 -	    ctx->unsaved_changes ? " " : " no ") == IO_SIGNAL) {
     1.9 -		return (CMD_SIGNAL);
    1.10 +	if (ctx->is_readonly) {
    1.11 +		if (io_printf("Read-only mode\n") == IO_SIGNAL) {
    1.12 +			return (CMD_SIGNAL);
    1.13 +		}
    1.14 +	} else {
    1.15 +		if (io_printf("There are%sunsaved changes\n",
    1.16 +		    ctx->unsaved_changes ? " " : " no ") == IO_SIGNAL) {
    1.17 +			return (CMD_SIGNAL);
    1.18 +		}
    1.19  	}
    1.20  
    1.21  	return (CMD_STATUS);
    1.22 @@ -495,6 +501,11 @@
    1.23  		goto out;
    1.24  	}
    1.25  
    1.26 +	if (ctx->is_readonly) {
    1.27 +		pwm_err(ctx, "cannot create new entries in read-only mode");
    1.28 +		goto out;
    1.29 +	}
    1.30 +
    1.31  	record = pwfile_create_record();
    1.32  
    1.33  	for (i = 1; i < argc; i++) {
    1.34 @@ -571,6 +582,12 @@
    1.35  		pwm_err(ctx, "invalid id %s", argv[1]);
    1.36  		goto out;
    1.37  	}
    1.38 +
    1.39 +	if (ctx->is_readonly) {
    1.40 +		pwm_err(ctx, "cannot modify entries in read-only mode");
    1.41 +		goto out;
    1.42 +	}
    1.43 +
    1.44  	record = pwfile_get_record(ctx, id);
    1.45  
    1.46  	for (i = 2; i < argc; i++) {
    1.47 @@ -650,6 +667,10 @@
    1.48  	/* check if first argument is an id */
    1.49  	if ((argc > 1) && (parse_id(argv[1], &id) == 0)) {
    1.50  		i++;
    1.51 +		if (ctx->is_readonly) {
    1.52 +			pwm_err(ctx, "cannot modify entries in read-only mode");
    1.53 +			goto out;
    1.54 +		}
    1.55  	}
    1.56  
    1.57  	for (; i < argc; i++) {
    1.58 @@ -781,6 +802,11 @@
    1.59  		return (CMD_ERR);
    1.60  	}
    1.61  
    1.62 +	if (ctx->is_readonly) {
    1.63 +		pwm_err(ctx, "cannot remove entries in read-only mode");
    1.64 +		return (CMD_ERR);
    1.65 +	}
    1.66 +
    1.67  	if (pwfile_remove_pws_record(ctx, id) != 0) {
    1.68  		pwm_err(ctx, "failed to remove record %u", id);
    1.69  		return (CMD_ERR);
    1.70 @@ -959,6 +985,11 @@
    1.71  		return (CMD_USAGE);
    1.72  	}
    1.73  
    1.74 +	if (ctx->is_readonly) {
    1.75 +		pwm_err(ctx, "cannot create groups in read-only mode");
    1.76 +		return (CMD_ERR);
    1.77 +	}
    1.78 +
    1.79  	if (ctx->is_interactive && (argc != 2)) {
    1.80  		if (io_get_line(NULL, "Group: ", 0, NULL, 0,
    1.81  		    sizeof (group_buf), group_buf) == IO_SIGNAL) {
    1.82 @@ -986,6 +1017,11 @@
    1.83  		return (CMD_USAGE);
    1.84  	}
    1.85  
    1.86 +	if (ctx->is_readonly) {
    1.87 +		pwm_err(ctx, "cannot remove groups in read-only mode");
    1.88 +		return (CMD_ERR);
    1.89 +	}
    1.90 +
    1.91  	if (pwfile_remove_group(ctx, argv[1]) != 0) {
    1.92  		pwm_err(ctx, "empty group \"%s\" does not exist", argv[1]);
    1.93  		return (CMD_ERR);
    1.94 @@ -1001,7 +1037,14 @@
    1.95  
    1.96  	if (argc > 2) {
    1.97  		return (CMD_USAGE);
    1.98 -	} else if (argc == 2) {
    1.99 +	}
   1.100 +
   1.101 +	if (ctx->is_readonly) {
   1.102 +		pwm_err(ctx, "cannot modify entries in read-only mode");
   1.103 +		return (CMD_ERR);
   1.104 +	}
   1.105 +
   1.106 +	if (argc == 2) {
   1.107  		len = strlen(argv[1]);
   1.108  		if (len == 0) {
   1.109  			pwm_err(ctx, "password must not be empty");
   1.110 @@ -1060,6 +1103,11 @@
   1.111  		return (CMD_USAGE);
   1.112  	}
   1.113  
   1.114 +	if (ctx->is_readonly) {
   1.115 +		pwm_err(ctx, "cannot write changes in read-only mode");
   1.116 +		return (CMD_ERR);
   1.117 +	}
   1.118 +
   1.119  	return ((pwfile_write_file(ctx) == 0) ? CMD_OK : CMD_ERR);
   1.120  }
   1.121  
     2.1 --- a/pwm.1.xml	Thu Sep 07 12:40:50 2017 +0200
     2.2 +++ b/pwm.1.xml	Sun Sep 17 18:45:05 2017 +0200
     2.3 @@ -34,7 +34,7 @@
     2.4        <email>guido+pwm@berhoerster.name</email>
     2.5        <personblurb/>
     2.6      </author>
     2.7 -    <date>7 September, 2017</date>
     2.8 +    <date>17 September, 2017</date>
     2.9    </info>
    2.10    <refmeta>
    2.11      <refentrytitle>pwm</refentrytitle>
    2.12 @@ -55,6 +55,9 @@
    2.13          <replaceable>password_file</replaceable>
    2.14        </arg>
    2.15        <arg choice="opt">
    2.16 +        <option>-R</option>
    2.17 +      </arg>
    2.18 +      <arg choice="opt">
    2.19          <replaceable>database_file</replaceable>
    2.20        </arg>
    2.21      </cmdsynopsis>
    2.22 @@ -107,6 +110,15 @@
    2.23            <replaceable>password_file</replaceable>.</para>
    2.24          </listitem>
    2.25        </varlistentry>
    2.26 +      <varlistentry>
    2.27 +        <term>
    2.28 +          <option>-R</option>
    2.29 +        </term>
    2.30 +        <listitem>
    2.31 +          <para>Treat the database as read-only and disallow any modifications
    2.32 +          and write operations.</para>
    2.33 +        </listitem>
    2.34 +      </varlistentry>
    2.35      </variablelist>
    2.36    </refsect1>
    2.37    <refsect1>
     3.1 --- a/pwm.c	Thu Sep 07 12:40:50 2017 +0200
     3.2 +++ b/pwm.c	Sun Sep 17 18:45:05 2017 +0200
     3.3 @@ -49,7 +49,7 @@
     3.4  static void
     3.5  usage(void)
     3.6  {
     3.7 -	fprintf(stderr, "usage: %s [-P file] [filename]\n", getprogname());
     3.8 +	fprintf(stderr, "usage: %s [-P file] [-R] [filename]\n", getprogname());
     3.9  }
    3.10  
    3.11  void
    3.12 @@ -344,11 +344,14 @@
    3.13  
    3.14  	ctx.is_interactive = isatty(STDIN_FILENO);
    3.15  
    3.16 -	while (!errflag && (c = getopt(argc, argv, "P:h")) != -1) {
    3.17 +	while (!errflag && (c = getopt(argc, argv, "P:Rh")) != -1) {
    3.18  		switch (c) {
    3.19  		case 'P':
    3.20  			master_password_filename = optarg;
    3.21  			break;
    3.22 +		case 'R':
    3.23 +			ctx.is_readonly = 1;
    3.24 +			break;
    3.25  		case 'h':
    3.26  			usage();
    3.27  			status = EXIT_SUCCESS;
     4.1 --- a/pwm.h	Thu Sep 07 12:40:50 2017 +0200
     4.2 +++ b/pwm.h	Sun Sep 17 18:45:05 2017 +0200
     4.3 @@ -45,6 +45,7 @@
     4.4  
     4.5  struct pwm_ctx {
     4.6  	int		is_interactive;
     4.7 +	int		is_readonly;
     4.8  	const char	*prev_cmd;
     4.9  	char		*errmsg;
    4.10  	char		*dirname;