1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
|
/*
* encode_keychange.c
*
* Collect information to build a KeyChange encoding, per the textual
* convention given in RFC 2274, Section 5. Compute the value and
* dump to stdout as a string of hex nibbles.
*
*
* Passphrase material may come from many sources. The following are
* checked in order (see get_user_passphrases()):
* - Prompt always if -f is given.
* - Commandline arguments.
* - PASSPHRASE_FILE.
* - Prompts on stdout. Use -P to turn off prompt tags.
*
*
* FIX Better name?
* FIX Change encode_keychange() to take random bits?
* FIX QUITFUN not quite appropriate here...
* FIX This is slow...
*/
#include <net-snmp/net-snmp-config.h>
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#include <net-snmp/net-snmp-includes.h>
#include <stdlib.h>
/*
* Globals, &c...
*/
char *local_progname;
char *local_passphrase_filename;
#define NL "\n"
#define USAGE "Usage: %s [-fhPvV] -t (md5|sha1) [-O \"<old_passphrase>\"][-N \"<new_passphrase>\"][-E [0x]<engineID>]"
#define OPTIONLIST "E:fhN:O:Pt:vVD"
#define PASSPHRASE_DIR ".snmp"
/*
* Rooted at $HOME.
*/
#define PASSPHRASE_FILE "passphrase.ek"
/*
* Format: two lines containing old and new passphrases, nothing more.
*
* XXX Add creature comforts like: comments and
* tokens identifying passphrases, separate directory check,
* check in current directory (?), traverse a path of
* directories (?)...
* FIX Better name?
*/
int forcepassphrase = 0, /* Always prompt for passphrases. */
promptindicator = 1, /* Output an indicator that input
* is requested. */
visible = 0, /* Echo passphrases to terminal. */
verbose = 0; /* Output progress to stderr. */
size_t engineid_len = 0;
u_char *engineid = NULL; /* Both input & final binary form. */
char *newpass = NULL, *oldpass = NULL;
char *transform_type_input = NULL;
const oid *transform_type = NULL; /* Type of HMAC hash to use. */
size_t transform_type_len = 0;
/*
* Prototypes.
*/
void usage_to_file(FILE * ofp);
void usage_synopsis(FILE * ofp);
int get_user_passphrases(void);
int snmp_ttyecho(const int fd, const int echo);
char *snmp_getpassphrase(const char *prompt, int fvisible);
#if defined(HAVE__CPUTS) && defined(HAVE__GETCH)
#include <conio.h>
#include <io.h>
#define HAVE_GETPASS 1
#define isatty _isatty
static char *getpass(const char *prompt);
#endif
/*******************************************************************-o-******
*/
int
main(int argc, char **argv)
{
int rval = 1;
size_t oldKu_len = SNMP_MAXBUF_SMALL,
newKu_len = SNMP_MAXBUF_SMALL,
oldkul_len = SNMP_MAXBUF_SMALL,
newkul_len = SNMP_MAXBUF_SMALL, keychange_len = SNMP_MAXBUF_SMALL;
char *s = NULL;
u_char oldKu[SNMP_MAXBUF_SMALL],
newKu[SNMP_MAXBUF_SMALL],
oldkul[SNMP_MAXBUF_SMALL],
newkul[SNMP_MAXBUF_SMALL], keychange[SNMP_MAXBUF_SMALL];
int i, auth_type;
int arg = 1;
local_progname = argv[0];
local_passphrase_filename = (char *) malloc(sizeof(PASSPHRASE_DIR) +
sizeof(PASSPHRASE_FILE) +
4);
if (!local_passphrase_filename) {
fprintf(stderr, "%s: out of memory!", local_progname);
exit(-1);
}
sprintf(local_passphrase_filename, "%s/%s", PASSPHRASE_DIR,
PASSPHRASE_FILE);
/*
* Parse.
*/
for (; (arg < argc) && (argv[arg][0] == '-'); arg++) {
switch (argv[arg][1]) {
case 'D':
snmp_set_do_debugging(1);
break;
case 'E':
engineid = (u_char *) argv[++arg];
break;
case 'f':
forcepassphrase = 1;
break;
case 'N':
newpass = argv[++arg];
break;
case 'O':
oldpass = argv[++arg];
break;
case 'P':
promptindicator = 0;
break;
case 't':
transform_type_input = argv[++arg];
break;
case 'v':
verbose = 1;
break;
case 'V':
visible = 1;
break;
case 'h':
rval = 0;
/* fallthrough */
default:
usage_to_file(stdout);
exit(rval);
}
}
if (!transform_type_input) {
fprintf(stderr, "The -t option is mandatory.\n");
usage_synopsis(stdout);
exit(1000);
}
/*
* Convert and error check transform_type.
*/
auth_type = usm_lookup_auth_type(transform_type_input);
transform_type = sc_get_auth_oid( auth_type, &transform_type_len );
if (NULL == transform_type) {
fprintf(stderr,
"Unrecognized hash transform: \"%s\".\n",
transform_type_input);
usage_synopsis(stderr);
QUITFUN(SNMPERR_GENERR, main_quit);
}
if (verbose) {
fprintf(stderr, "Hash:\t\t%s\n", sc_get_auth_name(auth_type));
}
/*
* Build engineID. Accept hex engineID as the bits
* "in-and-of-themselves", otherwise create an engineID with the
* given string as text.
*
* If no engineID is given, lookup the first IP address for the
* localhost and use that (see setup_engineID()).
*/
if (engineid && (tolower(*(engineid + 1)) == 'x')) {
engineid_len = hex_to_binary2(engineid + 2,
strlen((char *) engineid) - 2,
(char **) &engineid);
DEBUGMSGTL(("encode_keychange", "engineIDLen: %lu\n",
(unsigned long)engineid_len));
} else {
engineid_len = setup_engineID(&engineid, (char *) engineid);
if ((ssize_t)engineid_len < 0)
exit(1);
}
#ifdef NETSNMP_ENABLE_TESTING_CODE
if (verbose) {
fprintf(stderr, "EngineID:\t%s\n",
/*
* XXX =
*/ dump_snmpEngineID(engineid, &engineid_len));
}
#endif
/*
* Get passphrases from user.
*/
rval = get_user_passphrases();
QUITFUN(rval, main_quit);
if (strlen(oldpass) < USM_LENGTH_P_MIN) {
fprintf(stderr, "Old passphrase must be greater than %d "
"characters in length.\n", USM_LENGTH_P_MIN);
QUITFUN(SNMPERR_GENERR, main_quit);
} else if (strlen(newpass) < USM_LENGTH_P_MIN) {
fprintf(stderr, "New passphrase must be greater than %d "
"characters in length.\n", USM_LENGTH_P_MIN);
QUITFUN(SNMPERR_GENERR, main_quit);
}
if (verbose) {
fprintf(stderr,
"Old passphrase:\t%s\nNew passphrase:\t%s\n",
oldpass, newpass);
}
/*
* Compute Ku and Kul's from old and new passphrases, then
* compute the keychange string & print it out.
*/
rval = sc_init();
QUITFUN(rval, main_quit);
rval = generate_Ku(transform_type, transform_type_len,
(u_char *) oldpass, strlen(oldpass),
oldKu, &oldKu_len);
QUITFUN(rval, main_quit);
rval = generate_Ku(transform_type, transform_type_len,
(u_char *) newpass, strlen(newpass),
newKu, &newKu_len);
QUITFUN(rval, main_quit);
DEBUGMSGTL(("encode_keychange", "EID (%lu): ", (unsigned long)engineid_len));
for (i = 0; i < (int) engineid_len; i++)
DEBUGMSGTL(("encode_keychange", "%02x", (int) (engineid[i])));
DEBUGMSGTL(("encode_keychange", "\n"));
DEBUGMSGTL(("encode_keychange", "old Ku (%lu) (from %s): ", (unsigned long)oldKu_len,
oldpass));
for (i = 0; i < (int) oldKu_len; i++)
DEBUGMSGTL(("encode_keychange", "%02x", (int) (oldKu[i])));
DEBUGMSGTL(("encode_keychange", "\n"));
rval = generate_kul(transform_type, transform_type_len,
engineid, engineid_len,
oldKu, oldKu_len, oldkul, &oldkul_len);
QUITFUN(rval, main_quit);
DEBUGMSGTL(("encode_keychange", "generating old Kul (%lu) (from Ku): ",
(unsigned long)oldkul_len));
for (i = 0; i < (int) oldkul_len; i++)
DEBUGMSGTL(("encode_keychange", "%02x", (int) (oldkul[i])));
DEBUGMSGTL(("encode_keychange", "\n"));
rval = generate_kul(transform_type, transform_type_len,
engineid, engineid_len,
newKu, newKu_len, newkul, &newkul_len);
QUITFUN(rval, main_quit);
DEBUGMSGTL(("encode_keychange", "generating new Kul (%lu) (from Ku): ",
(unsigned long)oldkul_len));
for (i = 0; i < (int) newkul_len; i++)
DEBUGMSGTL(("encode_keychange", "%02x", newkul[i]));
DEBUGMSGTL(("encode_keychange", "\n"));
rval = encode_keychange(transform_type, transform_type_len,
oldkul, oldkul_len,
newkul, newkul_len, keychange, &keychange_len);
QUITFUN(rval, main_quit);
binary_to_hex(keychange, keychange_len, &s);
printf("%s%s\n", (verbose) ? "KeyChange string:\t" : "", /* XXX stdout */
s);
/*
* Cleanup.
*/
main_quit:
snmp_call_callbacks(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_SHUTDOWN,
NULL);
SNMP_ZERO(oldpass, strlen(oldpass));
SNMP_ZERO(newpass, strlen(newpass));
memset(oldKu, 0, oldKu_len);
memset(newKu, 0, newKu_len);
memset(oldkul, 0, oldkul_len);
memset(newkul, 0, newkul_len);
SNMP_ZERO(s, strlen(s));
return rval;
} /* end main() */
/*******************************************************************-o-******
*/
void
usage_synopsis(FILE * ofp)
{
fprintf(ofp, USAGE "\n\
\n\
-E [0x]<engineID> EngineID used for kul generation.\n\
-f Force passphrases to be read from stdin.\n\
-h Help.\n\
-N \"<new_passphrase>\" Passphrase used to generate new Ku.\n\
-O \"<old_passphrase>\" Passphrase used to generate old Ku.\n\
-P Turn off prompt indicators.\n\
-t md5 | sha1 HMAC hash transform type.\n\
-v Verbose.\n\
-V Visible. Echo passphrases to terminal.\n\
" NL, local_progname);
} /* end usage_synopsis() */
void
usage_to_file(FILE * ofp)
{
char *s;
usage_synopsis(ofp);
fprintf(ofp, "\n%s\
a) Commandline options,\n\
b) The file \"%s/%s\",\n\
c) stdin -or- User input from the terminal.\n\n%s\
" NL,
"Only -t is mandatory. The transform is used to convert P=>Ku, convert\n\
Ku=>Kul, and to hash the old Kul with the random bits.\n\
\n\
Passphrase will be taken from the first successful source as follows:\n",
(s = getenv("HOME")) ? s : "$HOME", local_passphrase_filename,
"-f will require reading from the stdin/terminal, ignoring a) and b).\n\
-P will prevent prompts for passphrases to stdout from being printed.\n\
\n\
<engineID> is interpreted as a hex string when preceded by \"0x\",\n\
otherwise it is created to contain \"text\". If nothing is given,\n\
<engineID> is constructed from the first IP address for the local host.\n");
/*
* FIX -- make this possible?
* -r [0x]<random_bits> Random bits used in KeyChange XOR.
*
* <engineID> and <random_bits> are interpreted as hex strings when
* preceeded by \"0x\", otherwise <engineID> is created to contain \"text\"
* and <random_bits> are the same as the ascii input.
*
* <random_bits> will be generated by SCAPI if not given. If value is
* too long, it will be truncated; if too short, the remainder will be
* filled in with zeros.
*/
} /* end usage() */
/*
* this defined for HPUX aCC because the aCC doesn't drop the
*/
/*
* snmp_parse_args.c functionality if compile with -g, PKY
*/
void
usage(void)
{
usage_to_file(stdout);
}
/*******************************************************************-o-******
* get_user_passphrases
*
* Returns:
* SNMPERR_SUCCESS Success.
* SNMPERR_GENERR Otherwise.
*
*
* Acquire new and old passphrases from the user:
*
* + Always prompt if 'forcepassphrase' is set.
* + Use given arguments if they are defined.
* + Otherwise read file format from PASSPHRASE_FILE.
* Sanity check existence and permissions of the path.
* ASSUME for now that PASSPHRASE_FILE is rooted only at $HOME.
* + Otherwise prompt user for passphrase(s).
* Echo input if 'visible' is set.
* Turning off 'promptindicator' makes piping in input cleaner.
*
* NOTE Only using forcepassphrase mandates taking both passphrases
* from the same source. Otherwise processing continues until both
* passphrases are defined.
*/
int
get_user_passphrases(void)
{
int rval = SNMPERR_SUCCESS;
size_t len;
char *obuf = NULL, *nbuf = NULL;
char path[SNMP_MAXBUF], buf[SNMP_MAXBUF], *s = NULL;
struct stat statbuf;
FILE *fp = NULL;
/*
* Allow prompts to the user to override all other sources.
* Nothing to do otherwise if oldpass and newpass are already defined.
*/
if (forcepassphrase)
goto get_user_passphrases_prompt;
if (oldpass && newpass)
goto get_user_passphrases_quit;
/*
* Read passphrases out of PASSPHRASE_FILE. Sanity check the
* path for existence and access first. Refuse to read
* if the permissions are wrong.
*/
s = getenv("HOME");
snprintf(path, sizeof(path), "%s/%s", s, PASSPHRASE_DIR);
path[ sizeof(path)-1 ] = 0;
/*
* Test directory.
*/
if (stat(path, &statbuf) < 0) {
fprintf(stderr, "Cannot access directory \"%s\".\n", path);
QUITFUN(SNMPERR_GENERR, get_user_passphrases_quit);
#ifndef WIN32
} else if (statbuf.st_mode & (S_IRWXG | S_IRWXO)) {
fprintf(stderr,
"Directory \"%s\" is accessible by group or world.\n",
path);
QUITFUN(SNMPERR_GENERR, get_user_passphrases_quit);
#endif /* !WIN32 */
}
/*
* Test file.
*/
snprintf(path, sizeof(path), "%s/%s", s, local_passphrase_filename);
path[ sizeof(path)-1 ] = 0;
if (stat(path, &statbuf) < 0) {
fprintf(stderr, "Cannot access file \"%s\".\n", path);
QUITFUN(SNMPERR_GENERR, get_user_passphrases_quit);
#ifndef WIN32
} else if (statbuf.st_mode & (S_IRWXG | S_IRWXO)) {
fprintf(stderr,
"File \"%s\" is accessible by group or world.\n", path);
QUITFUN(SNMPERR_GENERR, get_user_passphrases_quit);
#endif /* !WIN32 */
}
/*
* Open the file.
*/
if ((fp = fopen(path, "r")) == NULL) {
fprintf(stderr, "Cannot open \"%s\".", path);
QUITFUN(SNMPERR_GENERR, get_user_passphrases_quit);
}
/*
* Read 1st line.
*/
if (!fgets(buf, sizeof(buf), fp)) {
if (verbose) {
fprintf(stderr, "Passphrase file \"%s\" is empty...\n", path);
}
goto get_user_passphrases_prompt;
} else if (!oldpass) {
len = strlen(buf);
if (buf[len - 1] == '\n')
buf[--len] = '\0';
oldpass = (char *) calloc(1, len + 1);
if (oldpass)
memcpy(oldpass, buf, len + 1);
}
/*
* Read 2nd line.
*/
if (!fgets(buf, sizeof(buf), fp)) {
if (verbose) {
fprintf(stderr, "Only one line in file \"%s\"...\n", path);
}
} else if (!newpass) {
len = strlen(buf);
if (buf[len - 1] == '\n')
buf[--len] = '\0';
newpass = (char *) calloc(1, len + 1);
if (newpass)
memcpy(newpass, buf, len + 1);
}
if (oldpass && newpass)
goto get_user_passphrases_quit;
/*
* Prompt the user for passphrase entry. Visible prompts
* may be omitted, and invisible entry may turned off.
*/
get_user_passphrases_prompt:
if (forcepassphrase) {
oldpass = newpass = NULL;
}
if (!oldpass) {
oldpass = obuf
= snmp_getpassphrase((promptindicator) ? "Old passphrase: " :
"", visible);
}
if (!newpass) {
newpass = nbuf
= snmp_getpassphrase((promptindicator) ? "New passphrase: " :
"", visible);
}
/*
* Check that both passphrases were defined.
*/
if (oldpass && newpass) {
goto get_user_passphrases_quit;
} else {
rval = SNMPERR_GENERR;
}
get_user_passphrases_quit:
memset(buf, 0, SNMP_MAXBUF);
if (obuf != oldpass) {
SNMP_ZERO(obuf, strlen(obuf));
SNMP_FREE(obuf);
}
if (nbuf != newpass) {
SNMP_ZERO(nbuf, strlen(nbuf));
SNMP_FREE(nbuf);
}
if (fp)
fclose (fp);
return rval;
} /* end get_user_passphrases() */
/*******************************************************************-o-******
* snmp_ttyecho
*
* Parameters:
* fd Descriptor of terminal on which to toggle echoing.
* echo TRUE if echoing should be on; FALSE otherwise.
*
* Returns:
* Previous value of echo setting.
*
*
* FIX Put HAVE_TCGETATTR in autoconf?
*/
#ifndef HAVE_GETPASS
#ifdef HAVE_TCGETATTR
#include <termios.h>
int
snmp_ttyecho(const int fd, const int echo)
{
struct termios tio;
int was_echo;
if (!isatty(fd))
return (-1);
tcgetattr(fd, &tio);
was_echo = (tio.c_lflag & ECHO) != 0;
if (echo)
tio.c_lflag |= (ECHO | ECHONL);
else
tio.c_lflag &= ~(ECHO | ECHONL);
tcsetattr(fd, TCSANOW, &tio);
return (was_echo);
} /* end snmp_ttyecho() */
#else
#include <sgtty.h>
int
snmp_ttyecho(const int fd, const int echo)
{
struct sgttyb ttyparams;
int was_echo;
if (!isatty(fd))
was_echo = -1;
else {
ioctl(fd, TIOCGETP, &ttyparams);
was_echo = (ttyparams.sg_flags & ECHO) != 0;
if (echo)
ttyparams.sg_flags = ttyparams.sg_flags | ECHO;
else
ttyparams.sg_flags = ttyparams.sg_flags & ~ECHO;
ioctl(fd, TIOCSETP, &ttyparams);
}
return (was_echo);
} /* end snmp_ttyecho() */
#endif /* HAVE_TCGETATTR */
#endif /* HAVE_GETPASS */
/*******************************************************************-o-******
* snmp_getpassphrase
*
* Parameters:
* *prompt (May be NULL.)
* bvisible TRUE means echo back user input.
*
* Returns:
* Pointer to newly allocated, null terminated string containing
* passphrase -OR-
* NULL on error.
*
*
* Prompt stdin for a string (or passphrase). Return a copy of the
* input in a null terminated string.
*
* FIX Put HAVE_GETPASS in autoconf.
*/
char *
snmp_getpassphrase(const char *prompt, int bvisible)
{
int ti = 0;
size_t len;
char *bufp = NULL;
static char buffer[SNMP_MAXBUF];
FILE *ofp = stdout;
/*
* Query stdin for a passphrase.
*/
#ifdef HAVE_GETPASS
if (isatty(0)) {
return getpass((prompt) ? prompt : "");
}
#endif
fputs((prompt) ? prompt : "", ofp);
if (!bvisible) {
ti = snmp_ttyecho(0, 0);
}
bufp = fgets(buffer, sizeof(buffer), stdin);
if (!bvisible) {
ti = snmp_ttyecho(0, ti);
fputs("\n", ofp);
}
if (!bufp) {
fprintf(stderr, "Aborted...\n");
exit(1);
}
/*
* Copy the input and zero out the read-in buffer.
*/
len = strlen(buffer);
if (buffer[len - 1] == '\n')
buffer[--len] = '\0';
bufp = (char *) calloc(1, len + 1);
if (bufp)
memcpy(bufp, buffer, len + 1);
memset(buffer, 0, SNMP_MAXBUF);
return bufp;
} /* end snmp_getpassphrase() */
#if defined(HAVE__CPUTS) && defined(HAVE__GETCH)
int
snmp_ttyecho(const int fd, const int echo)
{
return 0;
}
/*
* stops at the first newline, carrier return, or backspace.
* WARNING! _getch does NOT read <Ctrl-C>
*/
static char *
getpass(const char *prompt)
{
static char pbuf[128];
int ch, lim;
_cputs(prompt);
for (ch = 0, lim = 0; ch != '\n' && lim < sizeof(pbuf)-1;) {
ch = _getch(); /* look ma, no echo ! */
if (ch == '\r' || ch == '\n' || ch == '\b')
break;
pbuf[lim++] = ch;
}
pbuf[lim] = '\0';
puts("\n");
return pbuf;
}
#endif /* defined(HAVE__CPUTS) && defined(HAVE__GETCH) */
|