view src/master/askpass.c @ 8590:b9faf4db2a9f HEAD

Updated copyright notices to include year 2009.
author Timo Sirainen <tss@iki.fi>
date Tue, 06 Jan 2009 09:25:38 -0500
parents 7ed926ed7aa4
children 00cd9aacd03c
line wrap: on
line source

/* Copyright (c) 2006-2009 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "askpass.h"

#include <stdio.h>
#include <termios.h>
#include <fcntl.h>
#include <unistd.h>

void askpass(const char *prompt, char *buf, size_t buf_size)
{
        struct termios old_tio, tio;
	bool restore_tio = FALSE;
	ssize_t ret;
	size_t pos;
	char ch;
	int fd;

	if (!isatty(STDIN_FILENO))
		i_fatal("stdin isn't a TTY");

	fputs(prompt, stderr);
	fflush(stderr);

	fd = open("/dev/tty", O_RDONLY);
	if (fd < 0)
		i_fatal("open(/dev/tty) failed: %m");

	/* turn off echo */
	if (tcgetattr(fd, &old_tio) == 0) {
		restore_tio = TRUE;
		tio = old_tio;
		tio.c_lflag &= ~(ECHO | ECHONL);
		(void)tcsetattr(fd, TCSAFLUSH, &tio);
	}

	/* read the password */
	pos = 0;
	while ((ret = read(fd, &ch, 1)) > 0) {
		if (pos >= buf_size-1)
			break;
		if (ch == '\n' || ch == '\r')
			break;
		buf[pos++] = ch;
	}
	buf[pos] = '\0';

	if (restore_tio)
		(void)tcsetattr(fd, TCSAFLUSH, &old_tio);

	fputs("\n", stderr); fflush(stderr);
	(void)close(fd);
}