Enable ctrl+c to abort command for the password prompt

This commit is contained in:
yixiangzhike 2024-12-06 09:56:44 +08:00
parent 7cad456b6b
commit c29796a9d8
2 changed files with 96 additions and 1 deletions

View File

@ -1,6 +1,6 @@
Name: adcli
Version: 0.9.0
Release: 2
Release: 3
Summary: A helper library and tools for Active Directory client operations
Group: Development/Libraries
License: LGPLv2+
@ -15,6 +15,7 @@ Patch4: 0005-add-option-use-ldaps.patch
Patch5: 0006-discovery-fix.patch
Patch6: 0007-delete-do-not-exit-if-keytab-cannot-be-read.patch
Patch7: 0008-tools-disable-SSSD-s-locator-plugin.patch
Patch8: backport-tools-replace-getpass.patch
BuildRequires: gcc intltool pkgconfig libtool gettext-devel krb5-devel
BuildRequires: openldap-devel libxslt xmlto git
@ -76,6 +77,9 @@ rm -rf %{buildroot}
%doc %{_mandir}/man8/*
%changelog
* Fri Dec 6 2024 yixiangzhike <yixiangzhike007@163.com> - 0.9.0-3
- backport upsteam patch to enable ctrl+c for the password prompt
* Mon Oct 17 2022 yixiangzhike <yixiangzhike007@163.com> - 0.9.0-2
- continue to build when test-case(test-adenroll) failed

View File

@ -0,0 +1,91 @@
From 054b24d5837cb32f94b6b659620caca2b567e4f6 Mon Sep 17 00:00:00 2001
From: Sumit Bose <sbose@redhat.com>
Date: Thu, 15 Sep 2022 18:19:19 +0200
Subject: [PATCH] tools: replace getpass()
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2124030
Resolves: https://gitlab.freedesktop.org/realmd/adcli/-/issues/10
---
tools/tools.c | 45 ++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 44 insertions(+), 1 deletion(-)
diff --git a/tools/tools.c b/tools/tools.c
index c78548b..7e382ae 100644
--- a/tools/tools.c
+++ b/tools/tools.c
@@ -36,6 +36,7 @@
#include <paths.h>
#include <stdio.h>
#include <unistd.h>
+#include <termios.h>
static char *adcli_temp_directory = NULL;
@@ -208,6 +209,47 @@ command_usage (void)
printf ("\nSee 'adcli <command> --help' for more information\n");
}
+static char *get_password (const char *prompt)
+{
+ int ret;
+ struct termios termios;
+ struct termios orig_termios;
+ char *buf = NULL;
+ size_t buf_len = 0;
+
+ ret = tcgetattr (fileno (stdin), &termios);
+ if (ret != 0 ) {
+ return NULL;
+ }
+
+ orig_termios = termios;
+ termios.c_lflag &= ~ECHO;
+
+ ret = tcsetattr (fileno (stdin), TCSAFLUSH, &termios);
+ if (ret != 0) {
+ return NULL;
+ }
+
+ fprintf (stdout, "%s", prompt);
+ fflush (stdout);
+
+ ret = getline (&buf, &buf_len, stdin);
+ tcsetattr (fileno (stdin), TCSAFLUSH, &orig_termios);
+ if (ret <= 0) {
+ free (buf);
+ return NULL;
+ }
+
+ if (buf[ret - 1] == '\n') {
+ /* remove new-line character from the end of the buffer and
+ * echo it to stdout */
+ buf[ret - 1] = '\0';
+ fprintf (stdout, "\n");
+ }
+
+ return buf;
+}
+
char *
adcli_prompt_password_func (adcli_login_type login_type,
const char *name,
@@ -221,7 +263,7 @@ adcli_prompt_password_func (adcli_login_type login_type,
if (asprintf (&prompt, "Password for %s: ", name) < 0)
return_val_if_reached (NULL);
- password = getpass (prompt);
+ password = get_password (prompt);
free (prompt);
if (password == NULL)
@@ -229,6 +271,7 @@ adcli_prompt_password_func (adcli_login_type login_type,
result = strdup (password);
adcli_mem_clear (password, strlen (password));
+ free (password);
return result;
}
--
2.33.0