Mercurial > dovecot > core-2.2
changeset 10003:3721ae3917fc HEAD
Moved askpass() from master/ to lib/. Added t_askpass().
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Thu, 08 Oct 2009 18:33:50 -0400 |
parents | d9cb9d03e50e |
children | 4c532d8222f3 |
files | src/lib/Makefile.am src/lib/askpass.c src/lib/askpass.h src/master/Makefile.am src/master/askpass.c src/master/askpass.h |
diffstat | 6 files changed, 71 insertions(+), 62 deletions(-) [+] |
line wrap: on
line diff
--- a/src/lib/Makefile.am Thu Oct 08 18:24:24 2009 -0400 +++ b/src/lib/Makefile.am Thu Oct 08 18:33:50 2009 -0400 @@ -11,6 +11,7 @@ liblib_la_SOURCES = \ array.c \ aqueue.c \ + askpass.c \ backtrace-string.c \ base64.c \ bsearch-insert-pos.c \ @@ -115,6 +116,7 @@ aqueue.h \ array.h \ array-decl.h \ + askpass.h \ backtrace-string.h \ base64.h \ bsearch-insert-pos.h \
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lib/askpass.c Thu Oct 08 18:33:50 2009 -0400 @@ -0,0 +1,62 @@ +/* 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); +} + +const char *t_askpass(const char *prompt) +{ + char buf[1024]; + + askpass(prompt, buf, sizeof(buf)); + return t_strdup(buf); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lib/askpass.h Thu Oct 08 18:33:50 2009 -0400 @@ -0,0 +1,7 @@ +#ifndef ASKPASS_H +#define ASKPASS_H + +void askpass(const char *prompt, char *buf, size_t buf_size); +const char *t_askpass(const char *prompt); + +#endif
--- a/src/master/Makefile.am Thu Oct 08 18:24:24 2009 -0400 +++ b/src/master/Makefile.am Thu Oct 08 18:33:50 2009 -0400 @@ -19,7 +19,6 @@ dovecot_DEPENDENCIES = $(libs) dovecot_SOURCES = \ - askpass.c \ capabilities-posix.c \ dup2-array.c \ main.c \ @@ -35,7 +34,6 @@ service.c noinst_HEADERS = \ - askpass.h \ capabilities.h \ common.h \ dup2-array.h \
--- a/src/master/askpass.c Thu Oct 08 18:24:24 2009 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,54 +0,0 @@ -/* 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); -}