Imported Upstream version 0.63.0
[hcoop/debian/courier-authlib.git] / authpasswd.c
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.3 2009/06/27 15:51:48 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 void sighandler(int signum)
33 {
34 if (write(1, "\n", 1) < 0)
35 ; /* Ignore gcc warning */
36 tcsetattr(0, TCSANOW, &tios);
37 _exit(0);
38 }
39 #endif
40
41 static void read_pw(char *buf)
42 {
43 int n, c;
44
45 n=0;
46 while ((c=getchar()) != EOF && c != '\n')
47 if (n < BUFSIZ-1)
48 buf[n++]=c;
49 if (c == EOF && n == 0) exit(1);
50 buf[n]=0;
51 }
52
53 int main(int argc, char **argv)
54 {
55 char buf[BUFSIZ];
56 char *p;
57 char hint[100];
58
59
60 strcpy(hint, "$1$");
61
62 if (argc > 1)
63 {
64 sprintf(hint, "{%1.15s}", argv[1]);
65 }
66
67 /* Read the password */
68 #if HAVE_TERMIOS_H
69
70 have_tios=0;
71 if (tcgetattr(0, &tios) == 0)
72 {
73 struct termios tios2;
74 char buf2[BUFSIZ];
75
76 have_tios=1;
77 signal(SIGINT, sighandler);
78 signal(SIGHUP, sighandler);
79 tios2=tios;
80 tios2.c_lflag &= ~ECHO;
81 tcsetattr(0, TCSANOW, &tios2);
82
83 for (;;)
84 {
85 if (write(2, "Password: ", 10) < 0)
86 ; /* Ignore gcc warning */
87 read_pw(buf);
88 if (write(2, "\nReenter password: ", 19) < 0)
89 ; /* Ignore gcc warning */
90 read_pw(buf2);
91 if (strcmp(buf, buf2) == 0) break;
92 if (write(2, "\nPasswords don't match.\n\n", 25) < 0)
93 ; /* Ignore gcc warning */
94
95 }
96
97 }
98 else
99 #endif
100 read_pw(buf);
101
102 #if HAVE_TERMIOS_H
103 if (have_tios)
104 {
105 if (write(2, "\n", 1) < 0)
106 ; /* Ignore gcc warning */
107 tcsetattr(0, TCSANOW, &tios);
108 signal(SIGINT, SIG_DFL);
109 signal(SIGHUP, SIG_DFL);
110 }
111 #endif
112
113 p=authcryptpasswd(buf, hint);
114
115 if (p)
116 printf("%s\n", p);
117 return (0);
118 }