11 /* Maximum number of characters allowed for input. */
12 #define MAX_INPUT_SIZE 32
14 void print_usage(const char prog[], int exit_code);
15 void set_tio_attr(int enable);
18 main(int argc, char *argv[])
21 #define PRINT_USAGE(exit_code) print_usage(argv[0], exit_code)
22 #define COMPARE_ARG(arg) (!strcmp(argv[1], arg))
25 PRINT_USAGE(EXIT_FAILURE);
26 else if (COMPARE_ARG("--help") || COMPARE_ARG("-h"))
27 PRINT_USAGE(EXIT_SUCCESS);
29 /* Prompt for password. */
30 else if (COMPARE_ARG("--prompt") || COMPARE_ARG("-p"))
32 char md5[MD5_PASSWD_LEN+1];
33 char buf[MAX_INPUT_SIZE+1];
38 if (!fgets(buf, (MAX_INPUT_SIZE+1), stdin))
42 fprintf(stderr, "Couldn't read input from stdin. (fgets(): %s)",
49 /* Remove LF at the end of line, if there is any. */
51 if (len > 0 && buf[len-1] == '\n')
57 pool_md5_hash(buf, len, md5);
58 printf("\n%s\n", md5);
61 /* Read password from argv[1]. */
64 char md5[MD5_PASSWD_LEN+1];
65 int len = strlen(argv[1]);
67 if (len > MAX_INPUT_SIZE)
69 fprintf(stderr, "Error: Input exceeds maximum password length!\n\n");
70 PRINT_USAGE(EXIT_FAILURE);
73 pool_md5_hash(argv[1], len, md5);
82 print_usage(const char prog[], int exit_code)
84 fprintf(((exit_code == EXIT_SUCCESS) ? stdout : stderr),
90 --prompt, -p Prompt password using standard input.\n\
91 --help, -h This help menu.\n\
93 Warning: At most %d characters are allowed for input.\n\
94 Warning: Plain password argument is deprecated for security concerns\n\
95 and kept for compatibility. Please prefer using password\n\
97 prog, prog, MAX_INPUT_SIZE);
104 set_tio_attr(int set)
107 static struct termios tio_save;
112 fprintf(stderr, "stdin is not tty\n");
118 if (tcgetattr(0, &tio) < 0)
120 fprintf(stderr, "set_tio_attr(set): tcgetattr failed\n");
126 tio.c_iflag &= ~(BRKINT|ISTRIP|IXON);
127 tio.c_lflag &= ~(ICANON|IEXTEN|ECHO|ECHOE|ECHOK|ECHONL);
131 if (tcsetattr(0, TCSANOW, &tio) < 0)
133 fprintf(stderr, "(set_tio_attr(set): tcsetattr failed\n");
139 if (tcsetattr(0, TCSANOW, &tio_save) < 0)
141 fprintf(stderr, "set_tio_attr(reset): tcsetattr failed\n");