diff options
| author | David M. Syzdek <david@syzdek.net> | 2022-06-02 12:28:29 -0800 |
|---|---|---|
| committer | David M. Syzdek <david@syzdek.net> | 2022-06-02 12:28:29 -0800 |
| commit | cb1c50c771bf3c60916d5a59c4928e296b926d9c (patch) | |
| tree | 91180399bb98af700464d53c2b31760049afef9e | |
| parent | 9bffc22fe1cc07d19f51e02665adc09cf30b06bb (diff) | |
| download | bindletools-cb1c50c771bf3c60916d5a59c4928e296b926d9c.tar.gz bindletools-cb1c50c771bf3c60916d5a59c4928e296b926d9c.tar.bz2 bindletools-cb1c50c771bf3c60916d5a59c4928e296b926d9c.tar.xz bindletools-cb1c50c771bf3c60916d5a59c4928e296b926d9c.zip | |
libbindle: adding bindle_getpass()
| -rw-r--r-- | build-aux/makefile-libbindle.am | 2 | ||||
| -rw-r--r-- | include/bindle.h | 10 | ||||
| -rw-r--r-- | lib/libbindle/lprompt.c | 116 | ||||
| -rw-r--r-- | lib/libbindle/lprompt.h | 45 | ||||
| -rw-r--r-- | m4/bindle-libbindle.m4 | 3 |
5 files changed, 176 insertions, 0 deletions
diff --git a/build-aux/makefile-libbindle.am b/build-aux/makefile-libbindle.am index e4294d2..7db0d52 100644 --- a/build-aux/makefile-libbindle.am +++ b/build-aux/makefile-libbindle.am | |||
| @@ -75,6 +75,8 @@ lib_libbindle_a_SOURCES = $(srcdir)/$(bindletools_srcdir)/include/bindle.h \ | |||
| 75 | $(srcdir)/$(bindletools_srcdir)/lib/libbindle/lobj.h \ | 75 | $(srcdir)/$(bindletools_srcdir)/lib/libbindle/lobj.h \ |
| 76 | $(srcdir)/$(bindletools_srcdir)/lib/libbindle/loptions.c \ | 76 | $(srcdir)/$(bindletools_srcdir)/lib/libbindle/loptions.c \ |
| 77 | $(srcdir)/$(bindletools_srcdir)/lib/libbindle/loptions.h \ | 77 | $(srcdir)/$(bindletools_srcdir)/lib/libbindle/loptions.h \ |
| 78 | $(srcdir)/$(bindletools_srcdir)/lib/libbindle/lprompt.c \ | ||
| 79 | $(srcdir)/$(bindletools_srcdir)/lib/libbindle/lprompt.h \ | ||
| 78 | $(srcdir)/$(bindletools_srcdir)/lib/libbindle/lstring.c \ | 80 | $(srcdir)/$(bindletools_srcdir)/lib/libbindle/lstring.c \ |
| 79 | $(srcdir)/$(bindletools_srcdir)/lib/libbindle/lstring.h \ | 81 | $(srcdir)/$(bindletools_srcdir)/lib/libbindle/lstring.h \ |
| 80 | $(srcdir)/$(bindletools_srcdir)/lib/libbindle/lstrings.c \ | 82 | $(srcdir)/$(bindletools_srcdir)/lib/libbindle/lstrings.c \ |
diff --git a/include/bindle.h b/include/bindle.h index 347dec9..cb7adec 100644 --- a/include/bindle.h +++ b/include/bindle.h | |||
| @@ -399,6 +399,16 @@ bindle_set_option( | |||
| 399 | const void * invalue ); | 399 | const void * invalue ); |
| 400 | 400 | ||
| 401 | 401 | ||
| 402 | //------------------------// | ||
| 403 | // user prompt prototypes // | ||
| 404 | //------------------------// | ||
| 405 | #pragma mark user prompt prototypes | ||
| 406 | |||
| 407 | char * | ||
| 408 | bindle_getpass( | ||
| 409 | const char * prompt ); | ||
| 410 | |||
| 411 | |||
| 402 | //-------------------// | 412 | //-------------------// |
| 403 | // string prototypes // | 413 | // string prototypes // |
| 404 | //-------------------// | 414 | //-------------------// |
diff --git a/lib/libbindle/lprompt.c b/lib/libbindle/lprompt.c new file mode 100644 index 0000000..8eeaddd --- /dev/null +++ b/lib/libbindle/lprompt.c | |||
| @@ -0,0 +1,116 @@ | |||
| 1 | /* | ||
| 2 | * Bindle Binaries Tools | ||
| 3 | * Copyright (C) 2022 David M. Syzdek <david@syzdek.net>. | ||
| 4 | * All rights reserved. | ||
| 5 | * | ||
| 6 | * Redistribution and use in source and binary forms, with or without | ||
| 7 | * modification, are permitted provided that the following conditions are | ||
| 8 | * met: | ||
| 9 | * | ||
| 10 | * * Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * * Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * * Neither the name of David M. Syzdek nor the | ||
| 16 | * names of its contributors may be used to endorse or promote products | ||
| 17 | * derived from this software without specific prior written permission. | ||
| 18 | * | ||
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | ||
| 20 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
| 21 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID M. SYZDEK BE LIABLE FOR | ||
| 23 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
| 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
| 26 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 29 | * SUCH DAMAGE. | ||
| 30 | */ | ||
| 31 | #define _LIB_LIBBINDLE_LPROMPT_C 1 | ||
| 32 | #include "lprompt.h" | ||
| 33 | |||
| 34 | |||
| 35 | /////////////// | ||
| 36 | // // | ||
| 37 | // Headers // | ||
| 38 | // // | ||
| 39 | /////////////// | ||
| 40 | #pragma mark - Headers | ||
| 41 | |||
| 42 | #include <stdio.h> | ||
| 43 | #include <signal.h> | ||
| 44 | #include <assert.h> | ||
| 45 | |||
| 46 | #ifdef HAVE_TERMIOS_H | ||
| 47 | #include <termios.h> | ||
| 48 | #endif | ||
| 49 | #ifdef HAVE_SGTTY_H | ||
| 50 | #include <sgtty.h> | ||
| 51 | #endif | ||
| 52 | |||
| 53 | |||
| 54 | ///////////////// | ||
| 55 | // // | ||
| 56 | // Functions // | ||
| 57 | // // | ||
| 58 | ///////////////// | ||
| 59 | #pragma mark - Functions | ||
| 60 | |||
| 61 | char * | ||
| 62 | bindle_getpass(const char * prompt) | ||
| 63 | { | ||
| 64 | static char buff[512]; | ||
| 65 | FILE * fs; | ||
| 66 | int c; | ||
| 67 | size_t pos; | ||
| 68 | #if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H) | ||
| 69 | struct termios old; | ||
| 70 | struct termios new; | ||
| 71 | void (*sig)( int sig ); | ||
| 72 | #endif | ||
| 73 | |||
| 74 | // prompts for password | ||
| 75 | prompt = ((prompt)) ? prompt : "Password: "; | ||
| 76 | if (prompt) | ||
| 77 | fprintf(stderr, "%s", prompt); | ||
| 78 | fflush(stdout); | ||
| 79 | |||
| 80 | // disables ECHO | ||
| 81 | #if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H) | ||
| 82 | if ((fs = fopen("/dev/tty", "r")) == NULL) | ||
| 83 | fs = stdin; | ||
| 84 | if(tcgetattr(fileno(fs), &old) == -1) | ||
| 85 | return(NULL); | ||
| 86 | sig = signal(SIGINT, SIG_IGN); | ||
| 87 | new = old; | ||
| 88 | new.c_lflag &= ~((unsigned)ECHO); | ||
| 89 | if(tcsetattr(fileno(fs), TCSANOW, &new)) | ||
| 90 | return(NULL); | ||
| 91 | #else | ||
| 92 | fs = stdin; | ||
| 93 | #endif | ||
| 94 | |||
| 95 | // reads buffer | ||
| 96 | pos = 0; | ||
| 97 | while ( ((c = getc(fs)) != EOF) && (c != '\n') && (c != '\r') ) | ||
| 98 | if (pos < (sizeof(buff)-1)) | ||
| 99 | buff[pos++] = (char)c; | ||
| 100 | buff[pos] = '\0'; | ||
| 101 | |||
| 102 | // restores previous terminal | ||
| 103 | fprintf(stderr, "\n"); | ||
| 104 | #if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H) | ||
| 105 | fflush(stderr); | ||
| 106 | if(tcsetattr(fileno(fs), TCSANOW, &old)) | ||
| 107 | return(NULL); | ||
| 108 | signal(SIGINT, sig); | ||
| 109 | fclose(fs); | ||
| 110 | #endif | ||
| 111 | |||
| 112 | return(buff); | ||
| 113 | } | ||
| 114 | |||
| 115 | |||
| 116 | /* end of source */ | ||
diff --git a/lib/libbindle/lprompt.h b/lib/libbindle/lprompt.h new file mode 100644 index 0000000..1d5785a --- /dev/null +++ b/lib/libbindle/lprompt.h | |||
| @@ -0,0 +1,45 @@ | |||
| 1 | /* | ||
| 2 | * Bindle Binaries Tools | ||
| 3 | * Copyright (C) 2022 David M. Syzdek <david@syzdek.net>. | ||
| 4 | * All rights reserved. | ||
| 5 | * | ||
| 6 | * Redistribution and use in source and binary forms, with or without | ||
| 7 | * modification, are permitted provided that the following conditions are | ||
| 8 | * met: | ||
| 9 | * | ||
| 10 | * * Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * * Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * * Neither the name of David M. Syzdek nor the | ||
| 16 | * names of its contributors may be used to endorse or promote products | ||
| 17 | * derived from this software without specific prior written permission. | ||
| 18 | * | ||
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | ||
| 20 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
| 21 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID M. SYZDEK BE LIABLE FOR | ||
| 23 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
| 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
| 26 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 29 | * SUCH DAMAGE. | ||
| 30 | */ | ||
| 31 | #ifndef _LIB_LIBBINDLE_LPROMPT_H | ||
| 32 | #define _LIB_LIBBINDLE_LPROMPT_H 1 | ||
| 33 | |||
| 34 | |||
| 35 | /////////////// | ||
| 36 | // // | ||
| 37 | // Headers // | ||
| 38 | // // | ||
| 39 | /////////////// | ||
| 40 | #pragma mark - Headers | ||
| 41 | |||
| 42 | #include "libbindle.h" | ||
| 43 | |||
| 44 | |||
| 45 | #endif /* end of header */ | ||
diff --git a/m4/bindle-libbindle.m4 b/m4/bindle-libbindle.m4 index c3e3851..8728121 100644 --- a/m4/bindle-libbindle.m4 +++ b/m4/bindle-libbindle.m4 | |||
| @@ -38,6 +38,9 @@ AC_DEFUN([AC_BINDLE_LIBBINDLE],[dnl | |||
| 38 | 38 | ||
| 39 | LIBBINDLE_PREFIX="$1" | 39 | LIBBINDLE_PREFIX="$1" |
| 40 | 40 | ||
| 41 | AC_CHECK_HEADERS([termios.h], [], []) | ||
| 42 | AC_CHECK_HEADERS([sgtty.h], [], []) | ||
| 43 | |||
| 41 | AC_SUBST([LIBBINDLE_PREFIX], [${LIBBINDLE_PREFIX}]) | 44 | AC_SUBST([LIBBINDLE_PREFIX], [${LIBBINDLE_PREFIX}]) |
| 42 | AC_DEFINE_UNQUOTED([LIBBINDLE_PREFIX], ["$LIBBINDLE_PREFIX"], [libbindle.la function prefix]) | 45 | AC_DEFINE_UNQUOTED([LIBBINDLE_PREFIX], ["$LIBBINDLE_PREFIX"], [libbindle.la function prefix]) |
| 43 | AM_CONDITIONAL([WITH_BINDLE_PREFIX_H], [test "x${LIBBINDLE_PREFIX}" != "x"]) | 46 | AM_CONDITIONAL([WITH_BINDLE_PREFIX_H], [test "x${LIBBINDLE_PREFIX}" != "x"]) |
