diff options
author | Mattias Andrée <maandree@operamail.com> | 2015-12-05 00:53:23 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2015-12-05 00:53:23 +0100 |
commit | 5aa11b712a9cf6279c41e6de93ee4d1355a0930b (patch) | |
tree | cd9fae9a0212f125637b1dbb9a9ebf8e0d90e94d | |
parent | accept input from any fd (diff) | |
download | libpassphrase-5aa11b712a9cf6279c41e6de93ee4d1355a0930b.tar.gz libpassphrase-5aa11b712a9cf6279c41e6de93ee4d1355a0930b.tar.bz2 libpassphrase-5aa11b712a9cf6279c41e6de93ee4d1355a0930b.tar.xz |
accept flags
Signed-off-by: Mattias Andrée <maandree@operamail.com>
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | info/libpassphrase.texinfo | 81 | ||||
-rw-r--r-- | src/passphrase.c | 8 | ||||
-rw-r--r-- | src/passphrase.h | 50 | ||||
-rw-r--r-- | src/test.c | 30 | ||||
-rw-r--r-- | src/test.h | 30 |
6 files changed, 146 insertions, 55 deletions
@@ -122,7 +122,7 @@ obj/passphrase.o: src/passphrase.c src/*.h @mkdir -p "$(shell dirname "$@")" $(CC) $(CC_FLAGS) -fPIC -o "$@" -c "$<" $(CFLAGS) $(CPPFLAGS) -obj/test.o: src/test.c src/test.h src/passphrase.h +obj/test.o: src/test.c src/passphrase.h @mkdir -p "$(shell dirname "$@")" $(CC) $(CC_FLAGS) -o "$@" -c "$<" $(CFLAGS) $(CPPFLAGS) diff --git a/info/libpassphrase.texinfo b/info/libpassphrase.texinfo index a5a27f1..d55d12d 100644 --- a/info/libpassphrase.texinfo +++ b/info/libpassphrase.texinfo @@ -50,10 +50,19 @@ Texts. A copy of the license is included in the section entitled @menu -* Overview:: Brief overview of libpassphrase. -* Advanced Programming Interface:: How to take advantage of libpassphrase in your application. -* Configuring libpassphrase:: How to configure libpassphrase. -* GNU Free Documentation License:: Copying and sharing this manual. +* Overview:: Brief overview of libpassphrase. +* Application Programming Interface:: How to take advantage of libpassphrase in your application. +* Configuring libpassphrase:: How to configure libpassphrase. +* GNU Free Documentation License:: Copying and sharing this manual. + +@detailmenu + --- The Detailed Node Listing --- + +Application Programming Interface + +* Example:: Example of how to use libpassphrase + +@end detailmenu @end menu @@ -77,11 +86,11 @@ or write your own replacement. -@node Advanced Programming Interface -@chapter Advanced Programming Interface +@node Application Programming Interface +@chapter Application Programming Interface @menu -* Example:: Example of how to use libpassphrase +* Example:: Example of how to use libpassphrase @end menu @@ -134,6 +143,37 @@ you should wipe it and free it. and is equivalent to @code{passphrase_read2(STDIN_FILENO, 0)}. +@code{flags} is used to tweak the behaviour +if the function. It should be a combination +of the constants +@table @code +@item PASSPHRASE_READ_EXISTING +@code{passphrase_read2} shall not do any thing +special, just accept the passphrase. This should +be used when getting authentication. Should not +be combined with @code{PASSPHRASE_READ_NEW}. +@item PASSPHRASE_READ_NEW +@code{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 @code{PASSPHRASE_READ_EXISTING}. +@item PASSPHRASE_READ_SCREEN_FREE +@code{passphrase_read2} may do as it please with +the the screen. This is only used if combined with +@code{PASSPHRASE_READ_NEW} and not with +@code{PASSPHRASE_READ_BELOW_FREE}. @code{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. +@item PASSPHRASE_READ_BELOW_FREE +@code{passphrase_read2} may do as it please with +the line below the current line. This is only used +if combined with @code{PASSPHRASE_READ_NEW}. +@code{passphrase_read2} will draw the passphrase +strength meter on the line below if such capability +is available. +@end table + @item void passphrase_reenable_echo1(int fdin) @itemx void passphrase_reenable_echo(void) When you have read the passphrase you should @@ -175,29 +215,41 @@ been disabled. @section Example @example -#include <passphrase.h> /* For libpassphrase */ -#include <stdio.h> /* For output */ -#include <stdlib.h> /* For free */ -#include <string.h> /* For strlen */ +#include <passphrase.h> /* For libpassphrase */ +#include <stdio.h> /* For output */ +#include <stdlib.h> /* For free */ +#include <string.h> /* For strlen */ +#include <fcntl.h> /* For open, O_RDONLY */ +#include <unistd.h> /* For close */ int main(int argc, char** argv) @{ /* Variables for the 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_echo(); + passphrase_disable_echo1(fd); /* Do things needed before reading the passphrase */ printf("Passphrase: "); fflush(stdout); /* Read the passphrase */ - passphrase = passphrase_read(); + 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; @} @@ -209,9 +261,10 @@ int main(int argc, char** argv) free(passphrase); /* Stop hiding user input */ - passphrase_reenable_echo(); + passphrase_reenable_echo1(fd); /* End of program */ + close(fd); return 0; /* `argc` was never used */ 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 <stddef.h> + #if defined(__GNUC__) && !defined(PASSPHRASE_USE_DEPRECATED) # define PASSPHRASE_DEPRECATED(MSG) __attribute__((__deprecated__(MSG))) #else @@ -28,6 +30,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 * * @return The passphrase, should be wiped and `free`:ed, `NULL` on error @@ -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); @@ -16,7 +16,13 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#include "test.h" +#include "passphrase.h" +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <fcntl.h> +#include <unistd.h> + /** @@ -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 <http://www.gnu.org/licenses/>. - */ -#ifndef TEST_H -#define TEST_H - - -#include <stdio.h> -#include <stdlib.h> - -#include "passphrase.h" - - -#endif - |