klinfix: add new commands password [generate | encrypt]

An excellent example of how to define commands with optional subcommands
and optional parameters:

  password encrypt [type <md5 | sha256 | sha512>] [salt STRING] [PASSWORD]

The tricks here are two:

  1. mode="switch" in the top-level password command
  2. min="0" in the encrypt subcommands

The first turns the COMMAND element into a SWITCH element (with command
matching), and the second makes a COMMAND optional.  If an optional
command is input by the user, then the enclosed PARAM is mandatory.

Please note, due to limitations in the mkpasswd¹ utility, spaces are not
supported in PASSWORD, i.e., when read from CLI prompt.  However, spaces
are allowed when using the interactive prompt, i.e., when omitting the
PASSWORD from the CLI prompt.

¹) mkpasswd does not look for any leading ' or " in the password arg.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2023-08-08 16:35:18 +02:00
committed by Tobias Waldekranz
parent a5ad52e5eb
commit 24d9a6dfc6
+28
View File
@@ -124,6 +124,34 @@
<ACTION sym="klix_copy@klinfix"/>
</COMMAND>
<COMMAND name="password" help="Password tools" mode="switch">
<COMMAND name="generate" help="Generate random passwords using pwgen">
<ACTION sym="script">pwgen -c -n -B -C</ACTION>
</COMMAND>
<COMMAND name="encrypt" help="Encrypt a password string">
<COMMAND name="salt" min="0" help="Random data to salt with before hashing.">
<PARAM name="pwsalt" ptype="/STRING" help="Must not use prefix like $1$"/>
</COMMAND>
<COMMAND name="type" min="0" help="Optional hash algorithm type.">
<PARAM name="pwhash" ptype="/STRING" help="Hash algorithm.">
<COMPL>
<ACTION sym="printl">md5</ACTION>
<ACTION sym="printl">sha256</ACTION>
<ACTION sym="printl">sha512</ACTION>
</COMPL>
</PARAM>
</COMMAND>
<PARAM name="pwpass" min="0" ptype="/STRING" help="Optional clear text password (no spaces)"/>
<ACTION sym="script" interactive="true">
type=${KLISH_PARAM_pwhash:-md5}
salt=${KLISH_PARAM_pwsalt:+-S $KLISH_PARAM_pwsalt}
mkpasswd -m $type $salt $KLISH_PARAM_pwpass
</ACTION>
</COMMAND>
</COMMAND>
<COMMAND name="set" help="Set" mode="switch">
<COMMAND name="datetime" help="Set current date and time">
<PARAM name="current-datetime" ptype="/STRING" help="yyyy-mm-ddThh:mm:ss(Z|+/-hh:mm)"/>