File: authpasswd.c

package info (click to toggle)
courier-authlib 0.58-4%2Betch3
  • links: PTS
  • area: main
  • in suites: etch
  • size: 16,212 kB
  • ctags: 1,896
  • sloc: ansic: 21,550; sh: 14,071; makefile: 866; perl: 842; cpp: 284
file content (115 lines) | stat: -rw-r--r-- 1,923 bytes parent folder | download | duplicates (2)
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
/*
** Copyright 2005 Double Precision, Inc.  See COPYING for
** distribution information.
*/

#if	HAVE_CONFIG_H
#include	"courier_auth_config.h"
#endif
#include	<string.h>
#if	HAVE_UNISTD_H
#include	<unistd.h>
#endif
#include	<stdlib.h>
#include	<stdio.h>
#if	HAVE_TERMIOS_H
#include	<termios.h>
#endif
#include	<signal.h>
#include	"auth.h"

static const char rcsid[]="$Id: authpasswd.c,v 1.1 2005/07/13 00:55:54 mrsam Exp $";
/*
**	Where possible, we turn off echo when entering the password.
**	We set up a signal handler to catch signals and restore the echo
**	prior to exiting.
*/

#if	HAVE_TERMIOS_H
static struct termios tios;
static int have_tios;

static RETSIGTYPE sighandler(int signum)
{
	write(1, "\n", 1);
	tcsetattr(0, TCSANOW, &tios);
	_exit(0);
#if	RETSIGTYPE != void
	return (0);
#endif
}
#endif

static void read_pw(char *buf)
{
int	n, c;

	n=0;
	while ((c=getchar()) != EOF && c != '\n')
		if (n < BUFSIZ-1)
			buf[n++]=c;
	if (c == EOF && n == 0)	exit(1);
	buf[n]=0;
}

int main(int argc, char **argv)
{
	char buf[BUFSIZ];
	char *p;
	char hint[100];


	strcpy(hint, "$1$");

	if (argc > 1)
	{
		sprintf(hint, "{%1.15s}", argv[1]);
	}

	/* Read the password */
#if	HAVE_TERMIOS_H

	have_tios=0;
	if (tcgetattr(0, &tios) == 0)
	{
		struct	termios tios2;
		char	buf2[BUFSIZ];

		have_tios=1;
		signal(SIGINT, sighandler);
		signal(SIGHUP, sighandler);
		tios2=tios;
		tios2.c_lflag &= ~ECHO;
		tcsetattr(0, TCSANOW, &tios2);

		for (;;)
		{
			write(2, "Password: ", 10);
			read_pw(buf);
			write(2, "\nReenter password: ", 19);
			read_pw(buf2);
			if (strcmp(buf, buf2) == 0)	break;
			write(2, "\nPasswords don't match.\n\n", 25);
		}

	}
	else
#endif
		read_pw(buf);

#if	HAVE_TERMIOS_H
	if (have_tios)
	{
		write(2, "\n", 1);
		tcsetattr(0, TCSANOW, &tios);
		signal(SIGINT, SIG_DFL);
		signal(SIGHUP, SIG_DFL);
	}
#endif

	p=authcryptpasswd(buf, hint);

	if (p)
		printf("%s\n", p);
	return (0);
}