]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
ask-password-tool: add --user/--system flag to systemd-ask-password tool
authorLennart Poettering <lennart@poettering.net>
Fri, 13 Sep 2024 11:45:08 +0000 (13:45 +0200)
committerLennart Poettering <lennart@poettering.net>
Mon, 21 Oct 2024 12:14:10 +0000 (14:14 +0200)
This allows selecting which agents to ask about this: system-level
agents, or per-user agents.

Fixes: #1232 #2217
man/systemd-ask-password.xml
src/ask-password/ask-password.c

index 91bc6f73668677bcdd3f112f182a726c712bbccd..fc005abed64ab1283e32bd79ab2e4d07b647688b 100644 (file)
   <refsect1>
     <title>Description</title>
 
-    <para><command>systemd-ask-password</command> may be used to query
-    a system password or passphrase from the user, using a question
-    message specified on the command line. When run from a TTY it will
-    query a password on the TTY and print it to standard output. When
-    run with no TTY or with <option>--no-tty</option> it will use the
-    system-wide query mechanism, which allows active users to respond via
-    several agents, listed below.</para>
-
-    <para>The purpose of this tool is to query system-wide passwords
-    — that is passwords not attached to a specific user account.
-    Examples include: unlocking encrypted hard disks when they are
-    plugged in or at boot, entering an SSL certificate passphrase for
-    web and VPN servers.</para>
-
-    <para>Existing agents are:
+    <para><command>systemd-ask-password</command> may be used to query a password or passphrase interactively
+    from the user, using a question prompt specified on the command line. When run from a TTY it will query a
+    password on the TTY and print it to standard output. When run with no TTY or with
+    <option>--no-tty</option> it will use a system-wide or per-user agent-based query mechanism, which allows
+    active users to respond via several agents, listed below.</para>
+
+    <para>The purpose of this tool is to query system-wide or per-user passwords — the former includes
+    passwords possibly not associated to a specific user account.  Examples include: unlocking encrypted hard
+    disks when they are plugged in or at boot, entering an SSL certificate passphrase for web and VPN
+    servers.</para>
+
+    <para>Existing system-level agents are:
     <itemizedlist>
 
-      <listitem><para>A boot-time password agent asking the user for
-      passwords using
-      <citerefentry project='die-net'><refentrytitle>plymouth</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
+      <listitem><para>A boot-time password agent asking the user for passwords using <citerefentry
+      project='die-net'><refentrytitle>plymouth</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
       </para></listitem>
 
       <listitem><para>A boot-time password agent querying the user
     all the agents listed above (except for the last one), run as privileged
     system services. The last one also needs elevated privileges, so
     should be run through
-    <citerefentry project='die-net'><refentrytitle>sudo</refentrytitle><manvolnum>8</manvolnum></citerefentry>
+    <citerefentry><refentrytitle>run0</refentrytitle><manvolnum>1</manvolnum></citerefentry>
     or similar.</para>
 
-    <para>Additional password agents may be implemented according to
-    the <ulink url="https://systemd.io/PASSWORD_AGENTS/">systemd Password Agent
-    Specification</ulink>.</para>
+    <para>Additional password agents may be implemented according to the <ulink
+    url="https://systemd.io/PASSWORD_AGENTS/">systemd Password Agent Specification</ulink>.</para>
 
     <para>If a password is queried on a TTY, the user may press TAB to
     hide the asterisks normally shown for each character typed.
     Pressing Backspace as first key achieves the same effect.</para>
-
   </refsect1>
 
   <refsect1>
         <xi:include href="version-info.xml" xpointer="v249"/></listitem>
       </varlistentry>
 
+      <varlistentry>
+        <term><option>--user</option></term>
+        <term><option>--system</option></term>
+
+        <listitem><para>Controls whether to query the system-wide or the per-user password agents. By default
+        if invoked privileged the system-wide agents are queried, otherwise the per-user ones. These options
+        allow to override this automatic behaviour.</para>
+
+        <xi:include href="version-info.xml" xpointer="v257"/></listitem>
+      </varlistentry>
+
       <xi:include href="standard-options.xml" xpointer="help" />
     </variablelist>
 
index b2c8ef70037ebf0b015bbb095b121b4ce2c7ffc8..59eb7acdddb3c43ce9702581f9bece931f325086 100644 (file)
@@ -38,7 +38,7 @@ static int help(void) {
                 return log_oom();
 
         printf("%1$s [OPTIONS...] MESSAGE\n\n"
-               "%3$sQuery the user for a system passphrase, via the TTY or a UI agent.%4$s\n\n"
+               "%3$sQuery the user for a passphrase, via the TTY or a UI agent.%4$s\n\n"
                "  -h --help           Show this help\n"
                "     --icon=NAME      Icon name\n"
                "     --id=ID          Query identifier (e.g. \"cryptsetup:/dev/sda5\")\n"
@@ -58,6 +58,8 @@ static int help(void) {
                "     --no-output      Do not print password to standard output\n"
                "  -n                  Do not suffix password written to standard output with\n"
                "                      newline\n"
+               "     --user           Ask only our own user's agents\n"
+               "     --system         Ask agents of the system and of all users\n"
                "\nSee the %2$s for details.\n",
                program_invocation_short_name,
                link,
@@ -81,6 +83,8 @@ static int parse_argv(int argc, char *argv[]) {
                 ARG_NO_OUTPUT,
                 ARG_VERSION,
                 ARG_CREDENTIAL,
+                ARG_USER,
+                ARG_SYSTEM,
         };
 
         static const struct option options[] = {
@@ -97,6 +101,8 @@ static int parse_argv(int argc, char *argv[]) {
                 { "keyname",       required_argument, NULL, ARG_KEYNAME       },
                 { "no-output",     no_argument,       NULL, ARG_NO_OUTPUT     },
                 { "credential",    required_argument, NULL, ARG_CREDENTIAL    },
+                { "user",          no_argument,       NULL, ARG_USER          },
+                { "system",        no_argument,       NULL, ARG_SYSTEM        },
                 {}
         };
 
@@ -183,6 +189,14 @@ static int parse_argv(int argc, char *argv[]) {
                         arg_credential_name = optarg;
                         break;
 
+                case ARG_USER:
+                        arg_flags |= ASK_PASSWORD_USER;
+                        break;
+
+                case ARG_SYSTEM:
+                        arg_flags &= ~ASK_PASSWORD_USER;
+                        break;
+
                 case 'n':
                         arg_newline = false;
                         break;
@@ -228,6 +242,9 @@ static int run(int argc, char *argv[]) {
 
         log_setup();
 
+        /* Unprivileged? Then imply ASK_PASSWORD_USER by default */
+        SET_FLAG(arg_flags, ASK_PASSWORD_USER, geteuid() != 0);
+
         r = parse_argv(argc, argv);
         if (r <= 0)
                 return r;