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