diff options
| author | David M. Syzdek <david@syzdek.net> | 2022-06-02 12:56:38 -0800 |
|---|---|---|
| committer | David M. Syzdek <david@syzdek.net> | 2022-06-02 12:56:38 -0800 |
| commit | 9b2da9d6e46c0dd446bd76150af5c610bcf7c55a (patch) | |
| tree | c833b76788e73405a19a2c4d62594e8fface079c | |
| parent | e991a32a7aade49d37a76596fac84a8170b73faf (diff) | |
| download | tinytac-9b2da9d6e46c0dd446bd76150af5c610bcf7c55a.tar.gz tinytac-9b2da9d6e46c0dd446bd76150af5c610bcf7c55a.tar.bz2 tinytac-9b2da9d6e46c0dd446bd76150af5c610bcf7c55a.tar.xz tinytac-9b2da9d6e46c0dd446bd76150af5c610bcf7c55a.zip | |
tinytac: adding options for retrieving password
| -rw-r--r-- | src/ttu/tinytacutil.c | 98 | ||||
| -rw-r--r-- | src/ttu/tinytacutil.h | 19 | ||||
| -rw-r--r-- | src/ttu/widget-authen.c | 8 |
3 files changed, 124 insertions, 1 deletions
diff --git a/src/ttu/tinytacutil.c b/src/ttu/tinytacutil.c index 3cd1f2b..e4b3ff5 100644 --- a/src/ttu/tinytacutil.c +++ b/src/ttu/tinytacutil.c | |||
| @@ -42,9 +42,13 @@ | |||
| 42 | #include <stdlib.h> | 42 | #include <stdlib.h> |
| 43 | #include <string.h> | 43 | #include <string.h> |
| 44 | #include <strings.h> | 44 | #include <strings.h> |
| 45 | #include <sys/stat.h> | ||
| 46 | #include <sys/types.h> | ||
| 47 | #include <sys/uio.h> | ||
| 45 | #include <unistd.h> | 48 | #include <unistd.h> |
| 46 | #include <getopt.h> | 49 | #include <getopt.h> |
| 47 | #include <unistd.h> | 50 | #include <unistd.h> |
| 51 | #include <fcntl.h> | ||
| 48 | #include <stdarg.h> | 52 | #include <stdarg.h> |
| 49 | #include <assert.h> | 53 | #include <assert.h> |
| 50 | #include <errno.h> | 54 | #include <errno.h> |
| @@ -299,10 +303,81 @@ ttu_cleanup( | |||
| 299 | ttu_config_t * cnf ) | 303 | ttu_config_t * cnf ) |
| 300 | { | 304 | { |
| 301 | tinytac_free(cnf->tt); | 305 | tinytac_free(cnf->tt); |
| 306 | if ((cnf->pass_buff)) | ||
| 307 | free(cnf->pass_buff); | ||
| 302 | return; | 308 | return; |
| 303 | } | 309 | } |
| 304 | 310 | ||
| 305 | 311 | ||
| 312 | char * | ||
| 313 | ttu_file2str( | ||
| 314 | const char * path ) | ||
| 315 | { | ||
| 316 | int fd; | ||
| 317 | struct stat sb; | ||
| 318 | char * str; | ||
| 319 | |||
| 320 | if (stat(path, &sb) == -1) | ||
| 321 | return(NULL); | ||
| 322 | |||
| 323 | if ((str = malloc(sb.st_size+1)) == NULL) | ||
| 324 | return(NULL); | ||
| 325 | |||
| 326 | if ((fd = open(path, O_RDONLY)) == -1) | ||
| 327 | { | ||
| 328 | free(str); | ||
| 329 | return(NULL); | ||
| 330 | } | ||
| 331 | |||
| 332 | if (read(fd, str, sb.st_size) == -1) | ||
| 333 | { | ||
| 334 | close(fd); | ||
| 335 | free(str); | ||
| 336 | return(NULL); | ||
| 337 | }; | ||
| 338 | |||
| 339 | str[sb.st_size] = '\0'; | ||
| 340 | close(fd); | ||
| 341 | |||
| 342 | return(str); | ||
| 343 | } | ||
| 344 | |||
| 345 | |||
| 346 | int | ||
| 347 | ttu_password( | ||
| 348 | ttu_config_t * cnf ) | ||
| 349 | { | ||
| 350 | const char * pass; | ||
| 351 | |||
| 352 | if ((cnf->pass)) | ||
| 353 | return(0); | ||
| 354 | |||
| 355 | // process password file | ||
| 356 | if ((cnf->pass_file)) | ||
| 357 | { | ||
| 358 | if ((cnf->pass_buff)) | ||
| 359 | free(cnf->pass_buff); | ||
| 360 | if ((cnf->pass_buff = ttu_file2str(cnf->pass_file)) == NULL) | ||
| 361 | return(ttu_error(cnf, 1, "%s: %s", cnf->pass_file, strerror(errno))); | ||
| 362 | cnf->pass = cnf->pass_buff; | ||
| 363 | }; | ||
| 364 | |||
| 365 | // prompt for password | ||
| 366 | if ((cnf->opts & TTUTILS_OPT_PASSPROMPT)) | ||
| 367 | { | ||
| 368 | if ((pass = tinytacb_getpass("Enter TACACS+ Password: ")) == NULL) | ||
| 369 | return(ttu_error(cnf, 1, "getpass: %s", strerror(errno))); | ||
| 370 | if ((cnf->pass_buff)) | ||
| 371 | free(cnf->pass_buff); | ||
| 372 | if ((cnf->pass_buff = tinytacb_strdup(pass)) == NULL) | ||
| 373 | return(ttu_error(cnf, 1, "%s", strerror(errno))); | ||
| 374 | cnf->pass = cnf->pass_buff; | ||
| 375 | }; | ||
| 376 | |||
| 377 | return(0); | ||
| 378 | } | ||
| 379 | |||
| 380 | |||
| 306 | //-----------------// | 381 | //-----------------// |
| 307 | // usage functions // | 382 | // usage functions // |
| 308 | //-----------------// | 383 | //-----------------// |
| @@ -320,7 +395,7 @@ ttu_cli_arguments( | |||
| 320 | int rc; | 395 | int rc; |
| 321 | 396 | ||
| 322 | // getopt options | 397 | // getopt options |
| 323 | static const char * short_opt = "+46dH:hVvq"; | 398 | static const char * short_opt = "+46dH:hVvqWw:y:"; |
| 324 | static struct option long_opt[] = | 399 | static struct option long_opt[] = |
| 325 | { | 400 | { |
| 326 | {"help", no_argument, NULL, 'h' }, | 401 | {"help", no_argument, NULL, 'h' }, |
| @@ -382,6 +457,24 @@ ttu_cli_arguments( | |||
| 382 | cnf->opts &= ~TTUTILS_OPT_QUIET; | 457 | cnf->opts &= ~TTUTILS_OPT_QUIET; |
| 383 | break; | 458 | break; |
| 384 | 459 | ||
| 460 | case 'W': | ||
| 461 | cnf->opts |= TTUTILS_OPT_PASSPROMPT; | ||
| 462 | cnf->pass = NULL; | ||
| 463 | cnf->pass_file = NULL; | ||
| 464 | break; | ||
| 465 | |||
| 466 | case 'w': | ||
| 467 | cnf->opts &= ~TTUTILS_OPT_PASSPROMPT; | ||
| 468 | cnf->pass = optarg; | ||
| 469 | cnf->pass_file = NULL; | ||
| 470 | break; | ||
| 471 | |||
| 472 | case 'y': | ||
| 473 | cnf->opts &= ~TTUTILS_OPT_PASSPROMPT; | ||
| 474 | cnf->pass = NULL; | ||
| 475 | cnf->pass_file = optarg; | ||
| 476 | break; | ||
| 477 | |||
| 385 | case '?': | 478 | case '?': |
| 386 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | 479 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); |
| 387 | return(1); | 480 | return(1); |
| @@ -423,6 +516,9 @@ ttu_usage( | |||
| 423 | printf(" -q, --quiet, --silent do not print messages\n"); | 516 | printf(" -q, --quiet, --silent do not print messages\n"); |
| 424 | printf(" -V, --version print version number and exit\n"); | 517 | printf(" -V, --version print version number and exit\n"); |
| 425 | printf(" -v, --verbose print verbose messages\n"); | 518 | printf(" -v, --verbose print verbose messages\n"); |
| 519 | printf(" -W prompt for authentication password\n"); | ||
| 520 | printf(" -w passwd authentication password\n"); | ||
| 521 | printf(" -y file read password from file\n"); | ||
| 426 | if (!(cnf->widget)) | 522 | if (!(cnf->widget)) |
| 427 | { | 523 | { |
| 428 | printf("WIDGETS:\n"); | 524 | printf("WIDGETS:\n"); |
diff --git a/src/ttu/tinytacutil.h b/src/ttu/tinytacutil.h index afc5471..1226618 100644 --- a/src/ttu/tinytacutil.h +++ b/src/ttu/tinytacutil.h | |||
| @@ -56,6 +56,7 @@ | |||
| 56 | 56 | ||
| 57 | #define TTUTILS_OPT_QUIET 0x00000001U | 57 | #define TTUTILS_OPT_QUIET 0x00000001U |
| 58 | #define TTUTILS_OPT_VERBOSE 0x00000002U | 58 | #define TTUTILS_OPT_VERBOSE 0x00000002U |
| 59 | #define TTUTILS_OPT_PASSPROMPT 0x00000004U | ||
| 59 | 60 | ||
| 60 | 61 | ||
| 61 | ////////////////// | 62 | ////////////////// |
| @@ -73,6 +74,9 @@ struct _tinytac_util_config | |||
| 73 | unsigned opts; | 74 | unsigned opts; |
| 74 | int argc; | 75 | int argc; |
| 75 | char ** argv; | 76 | char ** argv; |
| 77 | const char * pass; | ||
| 78 | const char * pass_file; | ||
| 79 | char * pass_buff; | ||
| 76 | const char * prog_name; | 80 | const char * prog_name; |
| 77 | const ttu_widget_t * widget; | 81 | const ttu_widget_t * widget; |
| 78 | TinyTac * tt; | 82 | TinyTac * tt; |
| @@ -134,6 +138,21 @@ tru_verbose( | |||
| 134 | ... ); | 138 | ... ); |
| 135 | 139 | ||
| 136 | 140 | ||
| 141 | //--------------------------// | ||
| 142 | // miscellaneous prototypes // | ||
| 143 | //--------------------------// | ||
| 144 | #pragma mark miscellaneous prototypes | ||
| 145 | |||
| 146 | extern char * | ||
| 147 | ttu_file2str( | ||
| 148 | const char * path ); | ||
| 149 | |||
| 150 | |||
| 151 | extern int | ||
| 152 | ttu_password( | ||
| 153 | ttu_config_t * cnf ); | ||
| 154 | |||
| 155 | |||
| 137 | //------------------// | 156 | //------------------// |
| 138 | // usage prototypes // | 157 | // usage prototypes // |
| 139 | //------------------// | 158 | //------------------// |
diff --git a/src/ttu/widget-authen.c b/src/ttu/widget-authen.c index 210fc83..0c722f9 100644 --- a/src/ttu/widget-authen.c +++ b/src/ttu/widget-authen.c | |||
| @@ -102,6 +102,14 @@ ttu_widget_authen( | |||
| 102 | // initial processing of cli arguments | 102 | // initial processing of cli arguments |
| 103 | if ((rc = ttu_cli_arguments(cnf, cnf->argc, cnf->argv)) != 0) | 103 | if ((rc = ttu_cli_arguments(cnf, cnf->argc, cnf->argv)) != 0) |
| 104 | return((rc == -1) ? 0 : 1); | 104 | return((rc == -1) ? 0 : 1); |
| 105 | if ((ttu_password(cnf))) | ||
| 106 | return(1); | ||
| 107 | if (!(cnf->pass)) | ||
| 108 | { | ||
| 109 | fprintf(stderr, "%s: missing required argument -W, -w, or -y\n", cnf->prog_name); | ||
| 110 | fprintf(stderr, "Try `%s --help' for more information.\n", cnf->prog_name); | ||
| 111 | return(1); | ||
| 112 | }; | ||
| 105 | 113 | ||
| 106 | return(0); | 114 | return(0); |
| 107 | } | 115 | } |
