| 1 | /* |
| 2 | ** Copyright 2005-2006 Double Precision, Inc. See COPYING for |
| 3 | ** distribution information. |
| 4 | */ |
| 5 | |
| 6 | #if HAVE_CONFIG_H |
| 7 | #include "courier_auth_config.h" |
| 8 | #endif |
| 9 | #include <string.h> |
| 10 | #if HAVE_UNISTD_H |
| 11 | #include <unistd.h> |
| 12 | #endif |
| 13 | #include <stdlib.h> |
| 14 | #include <stdio.h> |
| 15 | #if HAVE_TERMIOS_H |
| 16 | #include <termios.h> |
| 17 | #endif |
| 18 | #include <signal.h> |
| 19 | #include "auth.h" |
| 20 | |
| 21 | static const char rcsid[]="$Id: authpasswd.c,v 1.2 2006/06/01 10:47:32 mrsam Exp $"; |
| 22 | /* |
| 23 | ** Where possible, we turn off echo when entering the password. |
| 24 | ** We set up a signal handler to catch signals and restore the echo |
| 25 | ** prior to exiting. |
| 26 | */ |
| 27 | |
| 28 | #if HAVE_TERMIOS_H |
| 29 | static struct termios tios; |
| 30 | static int have_tios; |
| 31 | |
| 32 | static RETSIGTYPE sighandler(int signum) |
| 33 | { |
| 34 | if (write(1, "\n", 1) < 0) |
| 35 | ; /* Ignore gcc warning */ |
| 36 | tcsetattr(0, TCSANOW, &tios); |
| 37 | _exit(0); |
| 38 | #if RETSIGTYPE != void |
| 39 | return (0); |
| 40 | #endif |
| 41 | } |
| 42 | #endif |
| 43 | |
| 44 | static void read_pw(char *buf) |
| 45 | { |
| 46 | int n, c; |
| 47 | |
| 48 | n=0; |
| 49 | while ((c=getchar()) != EOF && c != '\n') |
| 50 | if (n < BUFSIZ-1) |
| 51 | buf[n++]=c; |
| 52 | if (c == EOF && n == 0) exit(1); |
| 53 | buf[n]=0; |
| 54 | } |
| 55 | |
| 56 | int main(int argc, char **argv) |
| 57 | { |
| 58 | char buf[BUFSIZ]; |
| 59 | char *p; |
| 60 | char hint[100]; |
| 61 | |
| 62 | |
| 63 | strcpy(hint, "$1$"); |
| 64 | |
| 65 | if (argc > 1) |
| 66 | { |
| 67 | sprintf(hint, "{%1.15s}", argv[1]); |
| 68 | } |
| 69 | |
| 70 | /* Read the password */ |
| 71 | #if HAVE_TERMIOS_H |
| 72 | |
| 73 | have_tios=0; |
| 74 | if (tcgetattr(0, &tios) == 0) |
| 75 | { |
| 76 | struct termios tios2; |
| 77 | char buf2[BUFSIZ]; |
| 78 | |
| 79 | have_tios=1; |
| 80 | signal(SIGINT, sighandler); |
| 81 | signal(SIGHUP, sighandler); |
| 82 | tios2=tios; |
| 83 | tios2.c_lflag &= ~ECHO; |
| 84 | tcsetattr(0, TCSANOW, &tios2); |
| 85 | |
| 86 | for (;;) |
| 87 | { |
| 88 | if (write(2, "Password: ", 10) < 0) |
| 89 | ; /* Ignore gcc warning */ |
| 90 | read_pw(buf); |
| 91 | if (write(2, "\nReenter password: ", 19) < 0) |
| 92 | ; /* Ignore gcc warning */ |
| 93 | read_pw(buf2); |
| 94 | if (strcmp(buf, buf2) == 0) break; |
| 95 | if (write(2, "\nPasswords don't match.\n\n", 25) < 0) |
| 96 | ; /* Ignore gcc warning */ |
| 97 | |
| 98 | } |
| 99 | |
| 100 | } |
| 101 | else |
| 102 | #endif |
| 103 | read_pw(buf); |
| 104 | |
| 105 | #if HAVE_TERMIOS_H |
| 106 | if (have_tios) |
| 107 | { |
| 108 | if (write(2, "\n", 1) < 0) |
| 109 | ; /* Ignore gcc warning */ |
| 110 | tcsetattr(0, TCSANOW, &tios); |
| 111 | signal(SIGINT, SIG_DFL); |
| 112 | signal(SIGHUP, SIG_DFL); |
| 113 | } |
| 114 | #endif |
| 115 | |
| 116 | p=authcryptpasswd(buf, hint); |
| 117 | |
| 118 | if (p) |
| 119 | printf("%s\n", p); |
| 120 | return (0); |
| 121 | } |