From 5aa11b712a9cf6279c41e6de93ee4d1355a0930b Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 5 Dec 2015 00:53:23 +0100 Subject: accept flags MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/passphrase.c | 8 +++++++- src/passphrase.h | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- src/test.c | 30 ++++++++++++++++++++++-------- src/test.h | 30 ------------------------------ 4 files changed, 78 insertions(+), 40 deletions(-) delete mode 100644 src/test.h (limited to 'src') diff --git a/src/passphrase.c b/src/passphrase.c index ec21aad..9c42c0f 100644 --- a/src/passphrase.c +++ b/src/passphrase.c @@ -129,7 +129,13 @@ char* passphrase_read(void) * Reads the passphrase from stdin * * @param fdin File descriptor for input - * @param flags Settings + * @param flags Settings, a combination of the constants: + * * PASSPHRASE_READ_EXISTING + * * PASSPHRASE_READ_NEW + * * PASSPHRASE_READ_SCREEN_FREE + * * PASSPHRASE_READ_BELOW_FREE + * Invalid input is ignored, to make use the + * application will work. * @return The passphrase, should be wiped and `free`:ed, `NULL` on error */ char* passphrase_read2(int fdin, int flags) diff --git a/src/passphrase.h b/src/passphrase.h index 24d4aa4..d24e66e 100644 --- a/src/passphrase.h +++ b/src/passphrase.h @@ -19,6 +19,8 @@ #ifndef PASSPHRASE_H #define PASSPHRASE_H +#include + #if defined(__GNUC__) && !defined(PASSPHRASE_USE_DEPRECATED) # define PASSPHRASE_DEPRECATED(MSG) __attribute__((__deprecated__(MSG))) #else @@ -27,6 +29,46 @@ +/** + * `passphrase_read2` shall not do any thing + * special, just accept the passphrase. This should + * be used when getting authentication. + * Should not be combined with `PASSPHRASE_READ_NEW`. + */ +#define PASSPHRASE_READ_EXISTING 0 + +/** + * `passphrase_read2` shall draw a pasphrase + * strenght meter if such capability is + * available. This should be used when create + * a new passphrase. + * Should not be combined with `PASSPHRASE_READ_EXISTING`. + */ +#define PASSPHRASE_READ_NEW 1 + +/** + * `passphrase_read2` may do as it please with the + * the screen. This is only used if combined with + * `PASSPHRASE_READ_NEW` and not with + * `PASSPHRASE_READ_BELOW_FREE`. `passphrase_read2` + * will create make a line below the new current + * line and use that line to draw the passphrase + * strength meter if such capability is available. + */ +#define PASSPHRASE_READ_SCREEN_FREE 2 + +/** + * `passphrase_read2` may do as it please with the + * line below the current line. This is only used + * if combined with `PASSPHRASE_READ_NEW`. + * `passphrase_read2` will draw the passphrase + * strength meter on the line below if such + * capability is available. + */ +#define PASSPHRASE_READ_BELOW_FREE 4 + + + /** * Reads the passphrase from stdin * @@ -39,7 +81,13 @@ char* passphrase_read(void); * Reads the passphrase * * @param fdin File descriptor for input - * @param flags Settings + * @param flags Settings, a combination of the constants: + * * PASSPHRASE_READ_EXISTING + * * PASSPHRASE_READ_NEW + * * PASSPHRASE_READ_SCREEN_FREE + * * PASSPHRASE_READ_BELOW_FREE + * Invalid input is ignored, to make use the + * application will work. * @return The passphrase, should be wiped and `free`:ed, `NULL` on error */ char* passphrase_read2(int, int); diff --git a/src/test.c b/src/test.c index e538316..36e9afc 100644 --- a/src/test.c +++ b/src/test.c @@ -16,7 +16,13 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -#include "test.h" +#include "passphrase.h" +#include +#include +#include +#include +#include + /** @@ -30,21 +36,30 @@ int main(int argc, char** argv) { /* Variables for the passphrase */ char* passphrase; - char* passphrase_; + + /* Get file descriptor to the terminal */ + int fd = open("/dev/tty", O_RDONLY); + if (fd == -1) + { + perror(*argv); + return 1; + } /* Hide the passphrase */ - passphrase_disable_echo1(0); + passphrase_disable_echo1(fd); /* Do things needed before reading the passphrase */ printf("Passphrase: "); fflush(stdout); /* Read the passphrase */ - passphrase = passphrase_read2(0, 0); + passphrase = passphrase_read2(fd, PASSPHRASE_READ_NEW | + PASSPHRASE_READ_SCREEN_FREE); if (passphrase == NULL) { /* Something went wrong, print what and exit */ perror(*argv); + close(fd); return 1; } @@ -52,15 +67,14 @@ int main(int argc, char** argv) printf("You entered: %s\n", passphrase); /* Wipe and free the passphrase */ - passphrase_ = passphrase; - while (*passphrase) - *passphrase++ = 0; - free(passphrase_); + passphrase_wipe(passphrase, strlen(passphrase)); + free(passphrase); /* Stop hiding user input */ passphrase_reenable_echo1(0); /* End of program */ + close(fd); return 0; /* `argc` was never used */ diff --git a/src/test.h b/src/test.h deleted file mode 100644 index 02f8de1..0000000 --- a/src/test.h +++ /dev/null @@ -1,30 +0,0 @@ -/** - * libpassphrase – Personalisable library for TTY passphrase reading - * - * Copyright © 2013, 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#ifndef TEST_H -#define TEST_H - - -#include -#include - -#include "passphrase.h" - - -#endif - -- cgit v1.2.3-70-g09d2