diff options
| author | David M. Syzdek <david@syzdek.net> | 2023-01-10 20:53:00 -0900 |
|---|---|---|
| committer | David M. Syzdek <david@syzdek.net> | 2023-01-10 20:53:00 -0900 |
| commit | d7d8f07c49bdc42f0f85cd4bc8c597850fbc874a (patch) | |
| tree | 5a57be1fa9779cb03dfba1d6bbf8138c19ba7e0c | |
| parent | 88c9236cabbba9b506c7a175081a8f8f73b05196 (diff) | |
| download | otputils-d7d8f07c49bdc42f0f85cd4bc8c597850fbc874a.tar.gz otputils-d7d8f07c49bdc42f0f85cd4bc8c597850fbc874a.tar.bz2 otputils-d7d8f07c49bdc42f0f85cd4bc8c597850fbc874a.tar.xz otputils-d7d8f07c49bdc42f0f85cd4bc8c597850fbc874a.zip | |
otp-data-convert: adding program
| -rw-r--r-- | Makefile.am | 14 | ||||
| -rw-r--r-- | tests/otp-data-convert.c | 220 |
2 files changed, 234 insertions, 0 deletions
diff --git a/Makefile.am b/Makefile.am index fa46421..abf3499 100644 --- a/Makefile.am +++ b/Makefile.am | |||
| @@ -82,6 +82,7 @@ sbin_PROGRAMS = | |||
| 82 | EXTRA_PROGRAMS = examples/totp-example \ | 82 | EXTRA_PROGRAMS = examples/totp-example \ |
| 83 | src/otp-altdict \ | 83 | src/otp-altdict \ |
| 84 | src/otputil \ | 84 | src/otputil \ |
| 85 | tests/otp-data-convert \ | ||
| 85 | tests/test-dict-otp \ | 86 | tests/test-dict-otp \ |
| 86 | tests/test-hotp \ | 87 | tests/test-hotp \ |
| 87 | tests/test-otp-decode \ | 88 | tests/test-otp-decode \ |
| @@ -137,6 +138,7 @@ DISTCHECK_CONFIGURE_FLAGS = --enable-strictwarnings \ | |||
| 137 | # conditional build | 138 | # conditional build |
| 138 | if ENABLE_MAINTAINER | 139 | if ENABLE_MAINTAINER |
| 139 | noinst_PROGRAMS += src/otp-altdict | 140 | noinst_PROGRAMS += src/otp-altdict |
| 141 | noinst_PROGRAMS += tests/otp-data-convert | ||
| 140 | endif | 142 | endif |
| 141 | if ENABLE_EXAMPLES | 143 | if ENABLE_EXAMPLES |
| 142 | noinst_PROGRAMS += examples/totp-example | 144 | noinst_PROGRAMS += examples/totp-example |
| @@ -216,6 +218,18 @@ src_otputil_SOURCES = src/otputil.c \ | |||
| 216 | src/widget-totp.c | 218 | src/widget-totp.c |
| 217 | 219 | ||
| 218 | 220 | ||
| 221 | # macros for tests/otp-data-convert | ||
| 222 | tests_otp_data_convert_DEPENDENCIES = Makefile $(noinst_LIBRARIES) $(lib_LTLIBRARIES) | ||
| 223 | tests_otp_data_convert_LDADD = $(AM_LIBS) $(noinst_LIBRARIES) $(lib_LTLIBRARIES) | ||
| 224 | tests_otp_data_convert_SOURCES = tests/otp-data-convert.c \ | ||
| 225 | tests/otp-dicts/otp-md4.c \ | ||
| 226 | tests/otp-dicts/otp-md5.c \ | ||
| 227 | tests/otp-dicts/otp-sha1.c \ | ||
| 228 | tests/otp-dicts/rfc2289-md4.c \ | ||
| 229 | tests/otp-dicts/rfc2289-md5.c \ | ||
| 230 | tests/otp-dicts/rfc2289-sha1.c | ||
| 231 | |||
| 232 | |||
| 219 | # macros for tests/test-dict-otp | 233 | # macros for tests/test-dict-otp |
| 220 | tests_test_dict_otp_DEPENDENCIES = Makefile $(noinst_LIBRARIES) $(lib_LTLIBRARIES) | 234 | tests_test_dict_otp_DEPENDENCIES = Makefile $(noinst_LIBRARIES) $(lib_LTLIBRARIES) |
| 221 | tests_test_dict_otp_LDADD = $(AM_LIBS) $(noinst_LIBRARIES) $(lib_LTLIBRARIES) | 235 | tests_test_dict_otp_LDADD = $(AM_LIBS) $(noinst_LIBRARIES) $(lib_LTLIBRARIES) |
diff --git a/tests/otp-data-convert.c b/tests/otp-data-convert.c new file mode 100644 index 0000000..a10f003 --- /dev/null +++ b/tests/otp-data-convert.c | |||
| @@ -0,0 +1,220 @@ | |||
| 1 | /* | ||
| 2 | * OTP Utilities | ||
| 3 | * Copyright (C) 2022 David M. Syzdek <david@syzdek.net>. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions are | ||
| 7 | * met: | ||
| 8 | * | ||
| 9 | * * Redistributions of source code must retain the above copyright | ||
| 10 | * notice, this list of conditions and the following disclaimer. | ||
| 11 | * * Redistributions in binary form must reproduce the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer in the | ||
| 13 | * documentation and/or other materials provided with the distribution. | ||
| 14 | * * Neither the name of David M. Syzdek nor the | ||
| 15 | * names of its contributors may be used to endorse or promote products | ||
| 16 | * derived from this software without specific prior written permission. | ||
| 17 | * | ||
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | ||
| 19 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
| 20 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 21 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID M SYZDEK BE LIABLE FOR | ||
| 22 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
| 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
| 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 28 | * SUCH DAMAGE. | ||
| 29 | */ | ||
| 30 | /* | ||
| 31 | * @file tests/otp-data-convert.c | ||
| 32 | */ | ||
| 33 | #define _TESTS_OTP_DATA_CONVERT_C 1 | ||
| 34 | |||
| 35 | /////////////// | ||
| 36 | // // | ||
| 37 | // Headers // | ||
| 38 | // // | ||
| 39 | /////////////// | ||
| 40 | #pragma mark - Headers | ||
| 41 | |||
| 42 | #include <otputil_compat.h> | ||
| 43 | |||
| 44 | #ifdef HAVE_CONFIG_H | ||
| 45 | # include <config.h> | ||
| 46 | #endif | ||
| 47 | |||
| 48 | #include <errno.h> | ||
| 49 | #include <assert.h> | ||
| 50 | #include <getopt.h> | ||
| 51 | #include <stdio.h> | ||
| 52 | #include <string.h> | ||
| 53 | #include <strings.h> | ||
| 54 | #include <inttypes.h> | ||
| 55 | |||
| 56 | #include <otputil.h> | ||
| 57 | |||
| 58 | #include "otp-dicts/otp-dicts.h" | ||
| 59 | |||
| 60 | |||
| 61 | /////////////////// | ||
| 62 | // // | ||
| 63 | // Definitions // | ||
| 64 | // // | ||
| 65 | /////////////////// | ||
| 66 | #pragma mark - Definitions | ||
| 67 | |||
| 68 | #ifndef PROGRAM_NAME | ||
| 69 | #define PROGRAM_NAME "otp-data-convert" | ||
| 70 | #endif | ||
| 71 | #ifndef PACKAGE_BUGREPORT | ||
| 72 | #define PACKAGE_BUGREPORT "david@syzdek.net" | ||
| 73 | #endif | ||
| 74 | #ifndef PACKAGE_COPYRIGHT | ||
| 75 | #define PACKAGE_COPYRIGHT "" | ||
| 76 | #endif | ||
| 77 | #ifndef PACKAGE_NAME | ||
| 78 | #define PACKAGE_NAME "" | ||
| 79 | #endif | ||
| 80 | #ifndef PACKAGE_VERSION | ||
| 81 | #define PACKAGE_VERSION "" | ||
| 82 | #endif | ||
| 83 | |||
| 84 | |||
| 85 | ///////////////// | ||
| 86 | // // | ||
| 87 | // Variables // | ||
| 88 | // // | ||
| 89 | ///////////////// | ||
| 90 | #pragma mark - Variables | ||
| 91 | |||
| 92 | static int verbose = 0; | ||
| 93 | static int quiet = 0; | ||
| 94 | |||
| 95 | |||
| 96 | ////////////////// | ||
| 97 | // // | ||
| 98 | // Prototypes // | ||
| 99 | // // | ||
| 100 | ////////////////// | ||
| 101 | #pragma mark - Prototypes | ||
| 102 | |||
| 103 | int | ||
| 104 | main( | ||
| 105 | int argc, | ||
| 106 | char * argv[] ); | ||
| 107 | |||
| 108 | |||
| 109 | ///////////////// | ||
| 110 | // // | ||
| 111 | // Functions // | ||
| 112 | // // | ||
| 113 | ///////////////// | ||
| 114 | #pragma mark - Functions | ||
| 115 | |||
| 116 | int | ||
| 117 | main( | ||
| 118 | int argc, | ||
| 119 | char * argv[] ) | ||
| 120 | { | ||
| 121 | int c; | ||
| 122 | int opt_index; | ||
| 123 | int method; | ||
| 124 | int val; | ||
| 125 | |||
| 126 | // getopt options | ||
| 127 | static const char * short_opt = "hqVv"; | ||
| 128 | static struct option long_opt[] = | ||
| 129 | { | ||
| 130 | {"help", no_argument, NULL, 'h' }, | ||
| 131 | {"quiet", no_argument, NULL, 'q' }, | ||
| 132 | {"silent", no_argument, NULL, 'q' }, | ||
| 133 | {"version", no_argument, NULL, 'V' }, | ||
| 134 | {"verbose", no_argument, NULL, 'v' }, | ||
| 135 | { NULL, 0, NULL, 0 } | ||
| 136 | }; | ||
| 137 | |||
| 138 | while((c = getopt_long(argc, argv, short_opt, long_opt, &opt_index)) != -1) | ||
| 139 | { | ||
| 140 | switch(c) | ||
| 141 | { | ||
| 142 | case -1: /* no more arguments */ | ||
| 143 | case 0: /* long options toggles */ | ||
| 144 | break; | ||
| 145 | |||
| 146 | case 'h': | ||
| 147 | printf("Usage: %s [OPTIONS] <hash> <skey word>\n", PROGRAM_NAME); | ||
| 148 | printf("OPTIONS:\n"); | ||
| 149 | printf(" -h, --help print this help and exit\n"); | ||
| 150 | printf(" -q, --quiet, --silent do not print messages\n"); | ||
| 151 | printf(" -V, --version print version number and exit\n"); | ||
| 152 | printf(" -v, --verbose print verbose messages\n"); | ||
| 153 | printf("\n"); | ||
| 154 | return(0); | ||
| 155 | |||
| 156 | case 'q': | ||
| 157 | quiet++; | ||
| 158 | break; | ||
| 159 | |||
| 160 | case 'V': | ||
| 161 | printf("%s (%s) %s\n", PROGRAM_NAME, PACKAGE_NAME, PACKAGE_VERSION); | ||
| 162 | printf("Written by David M. Syzdek.\n"); | ||
| 163 | return(0); | ||
| 164 | |||
| 165 | case 'v': | ||
| 166 | verbose++; | ||
| 167 | break; | ||
| 168 | |||
| 169 | case '?': | ||
| 170 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | ||
| 171 | return(1); | ||
| 172 | |||
| 173 | default: | ||
| 174 | fprintf(stderr, "%s: unrecognized option `--%c'\n", PROGRAM_NAME, c); | ||
| 175 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | ||
| 176 | return(1); | ||
| 177 | }; | ||
| 178 | }; | ||
| 179 | if (argc > 3) | ||
| 180 | { | ||
| 181 | fprintf(stderr, "%s: unknown argument -- `%s'\n", PROGRAM_NAME, argv[3]); | ||
| 182 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | ||
| 183 | return(1); | ||
| 184 | }; | ||
| 185 | if (argc < 3) | ||
| 186 | { | ||
| 187 | fprintf(stderr, "%s: missing required argument\n", PROGRAM_NAME); | ||
| 188 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | ||
| 189 | return(1); | ||
| 190 | }; | ||
| 191 | if ((method = otputil_str2md(argv[1])) == -1) | ||
| 192 | { | ||
| 193 | fprintf(stderr, "%s: unknown hash method -- `%s'\n", PROGRAM_NAME, argv[1]); | ||
| 194 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | ||
| 195 | return(1); | ||
| 196 | }; | ||
| 197 | if ((val = otputil_skey_dict_value(argv[2])) == -1) | ||
| 198 | { | ||
| 199 | fprintf(stderr, "%s: unknown S/KEY dictionary word -- `%s'\n", PROGRAM_NAME, argv[2]); | ||
| 200 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | ||
| 201 | return(1); | ||
| 202 | }; | ||
| 203 | |||
| 204 | switch(method) | ||
| 205 | { | ||
| 206 | case OTPUTIL_MD_MD4: printf("%s\n", otputil_dict_rfc2289_md4[val]); break; | ||
| 207 | case OTPUTIL_MD_MD5: printf("%s\n", otputil_dict_rfc2289_md5[val]); break; | ||
| 208 | case OTPUTIL_MD_SHA1: printf("%s\n", otputil_dict_rfc2289_sha1[val]); break; | ||
| 209 | |||
| 210 | default: | ||
| 211 | fprintf(stderr, "%s: unknown supported hash -- `%s'\n", PROGRAM_NAME, argv[1]); | ||
| 212 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | ||
| 213 | return(1); | ||
| 214 | }; | ||
| 215 | |||
| 216 | return(0); | ||
| 217 | } | ||
| 218 | |||
| 219 | |||
| 220 | /* end of source file */ | ||
