annotate src/lib/askpass.c @ 22955:812e5c961328

fts: Indexing virtual mailbox didn't always index the last mails
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Thu, 03 May 2018 18:33:00 +0300
parents cb108f786fb4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
22713
cb108f786fb4 Updated copyright notices to include the year 2018.
Stephan Bosch <stephan.bosch@dovecot.fi>
parents: 21390
diff changeset
1 /* Copyright (c) 2006-2018 Dovecot authors, see the included COPYING file */
10003
3721ae3917fc Moved askpass() from master/ to lib/. Added t_askpass().
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
2
3721ae3917fc Moved askpass() from master/ to lib/. Added t_askpass().
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
3 #include "lib.h"
12481
6ea1671108f1 t_askpass(): Removed a limit of 1024 bytes for the password.
Timo Sirainen <tss@iki.fi>
parents: 11039
diff changeset
4 #include "buffer.h"
6ea1671108f1 t_askpass(): Removed a limit of 1024 bytes for the password.
Timo Sirainen <tss@iki.fi>
parents: 11039
diff changeset
5 #include "str.h"
10003
3721ae3917fc Moved askpass() from master/ to lib/. Added t_askpass().
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
6 #include "askpass.h"
3721ae3917fc Moved askpass() from master/ to lib/. Added t_askpass().
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
7
3721ae3917fc Moved askpass() from master/ to lib/. Added t_askpass().
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
8 #include <stdio.h>
3721ae3917fc Moved askpass() from master/ to lib/. Added t_askpass().
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
9 #include <termios.h>
3721ae3917fc Moved askpass() from master/ to lib/. Added t_askpass().
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
10 #include <fcntl.h>
3721ae3917fc Moved askpass() from master/ to lib/. Added t_askpass().
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
11 #include <unistd.h>
3721ae3917fc Moved askpass() from master/ to lib/. Added t_askpass().
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
12
12481
6ea1671108f1 t_askpass(): Removed a limit of 1024 bytes for the password.
Timo Sirainen <tss@iki.fi>
parents: 11039
diff changeset
13 static void askpass_str(const char *prompt, buffer_t *pass)
10003
3721ae3917fc Moved askpass() from master/ to lib/. Added t_askpass().
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
14 {
3721ae3917fc Moved askpass() from master/ to lib/. Added t_askpass().
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
15 struct termios old_tio, tio;
12483
719ce27f9955 askpass: Allow reading password from stdin even if it's not a tty.
Timo Sirainen <tss@iki.fi>
parents: 12481
diff changeset
16 bool tty, restore_tio = FALSE;
10003
3721ae3917fc Moved askpass() from master/ to lib/. Added t_askpass().
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
17 char ch;
3721ae3917fc Moved askpass() from master/ to lib/. Added t_askpass().
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
18 int fd;
3721ae3917fc Moved askpass() from master/ to lib/. Added t_askpass().
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
19
12483
719ce27f9955 askpass: Allow reading password from stdin even if it's not a tty.
Timo Sirainen <tss@iki.fi>
parents: 12481
diff changeset
20 tty = isatty(STDIN_FILENO);
719ce27f9955 askpass: Allow reading password from stdin even if it's not a tty.
Timo Sirainen <tss@iki.fi>
parents: 12481
diff changeset
21 if (tty) {
719ce27f9955 askpass: Allow reading password from stdin even if it's not a tty.
Timo Sirainen <tss@iki.fi>
parents: 12481
diff changeset
22 fputs(prompt, stderr);
719ce27f9955 askpass: Allow reading password from stdin even if it's not a tty.
Timo Sirainen <tss@iki.fi>
parents: 12481
diff changeset
23 fflush(stderr);
10003
3721ae3917fc Moved askpass() from master/ to lib/. Added t_askpass().
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
24
12483
719ce27f9955 askpass: Allow reading password from stdin even if it's not a tty.
Timo Sirainen <tss@iki.fi>
parents: 12481
diff changeset
25 fd = open("/dev/tty", O_RDONLY);
719ce27f9955 askpass: Allow reading password from stdin even if it's not a tty.
Timo Sirainen <tss@iki.fi>
parents: 12481
diff changeset
26 if (fd < 0)
719ce27f9955 askpass: Allow reading password from stdin even if it's not a tty.
Timo Sirainen <tss@iki.fi>
parents: 12481
diff changeset
27 i_fatal("open(/dev/tty) failed: %m");
10003
3721ae3917fc Moved askpass() from master/ to lib/. Added t_askpass().
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
28
12483
719ce27f9955 askpass: Allow reading password from stdin even if it's not a tty.
Timo Sirainen <tss@iki.fi>
parents: 12481
diff changeset
29 /* turn off echo */
719ce27f9955 askpass: Allow reading password from stdin even if it's not a tty.
Timo Sirainen <tss@iki.fi>
parents: 12481
diff changeset
30 if (tcgetattr(fd, &old_tio) == 0) {
719ce27f9955 askpass: Allow reading password from stdin even if it's not a tty.
Timo Sirainen <tss@iki.fi>
parents: 12481
diff changeset
31 restore_tio = TRUE;
719ce27f9955 askpass: Allow reading password from stdin even if it's not a tty.
Timo Sirainen <tss@iki.fi>
parents: 12481
diff changeset
32 tio = old_tio;
719ce27f9955 askpass: Allow reading password from stdin even if it's not a tty.
Timo Sirainen <tss@iki.fi>
parents: 12481
diff changeset
33 tio.c_lflag &= ~(ECHO | ECHONL);
719ce27f9955 askpass: Allow reading password from stdin even if it's not a tty.
Timo Sirainen <tss@iki.fi>
parents: 12481
diff changeset
34 (void)tcsetattr(fd, TCSAFLUSH, &tio);
719ce27f9955 askpass: Allow reading password from stdin even if it's not a tty.
Timo Sirainen <tss@iki.fi>
parents: 12481
diff changeset
35 }
719ce27f9955 askpass: Allow reading password from stdin even if it's not a tty.
Timo Sirainen <tss@iki.fi>
parents: 12481
diff changeset
36 } else {
719ce27f9955 askpass: Allow reading password from stdin even if it's not a tty.
Timo Sirainen <tss@iki.fi>
parents: 12481
diff changeset
37 /* read it from stdin without showing a prompt */
719ce27f9955 askpass: Allow reading password from stdin even if it's not a tty.
Timo Sirainen <tss@iki.fi>
parents: 12481
diff changeset
38 fd = STDIN_FILENO;
10003
3721ae3917fc Moved askpass() from master/ to lib/. Added t_askpass().
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
39 }
3721ae3917fc Moved askpass() from master/ to lib/. Added t_askpass().
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
40
3721ae3917fc Moved askpass() from master/ to lib/. Added t_askpass().
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
41 /* read the password */
11039
0f98525e4567 Removed dead code.
Timo Sirainen <tss@iki.fi>
parents: 10582
diff changeset
42 while (read(fd, &ch, 1) > 0) {
10003
3721ae3917fc Moved askpass() from master/ to lib/. Added t_askpass().
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
43 if (ch == '\n' || ch == '\r')
3721ae3917fc Moved askpass() from master/ to lib/. Added t_askpass().
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
44 break;
12481
6ea1671108f1 t_askpass(): Removed a limit of 1024 bytes for the password.
Timo Sirainen <tss@iki.fi>
parents: 11039
diff changeset
45 buffer_append_c(pass, ch);
10003
3721ae3917fc Moved askpass() from master/ to lib/. Added t_askpass().
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
46 }
3721ae3917fc Moved askpass() from master/ to lib/. Added t_askpass().
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
47
12483
719ce27f9955 askpass: Allow reading password from stdin even if it's not a tty.
Timo Sirainen <tss@iki.fi>
parents: 12481
diff changeset
48 if (tty) {
719ce27f9955 askpass: Allow reading password from stdin even if it's not a tty.
Timo Sirainen <tss@iki.fi>
parents: 12481
diff changeset
49 if (restore_tio)
719ce27f9955 askpass: Allow reading password from stdin even if it's not a tty.
Timo Sirainen <tss@iki.fi>
parents: 12481
diff changeset
50 (void)tcsetattr(fd, TCSAFLUSH, &old_tio);
10003
3721ae3917fc Moved askpass() from master/ to lib/. Added t_askpass().
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
51
12483
719ce27f9955 askpass: Allow reading password from stdin even if it's not a tty.
Timo Sirainen <tss@iki.fi>
parents: 12481
diff changeset
52 fputs("\n", stderr); fflush(stderr);
14691
3945a3646c67 Changed i_close_fd() API to set the fd to -1 after closing.
Timo Sirainen <tss@iki.fi>
parents: 14687
diff changeset
53 i_close_fd(&fd);
12483
719ce27f9955 askpass: Allow reading password from stdin even if it's not a tty.
Timo Sirainen <tss@iki.fi>
parents: 12481
diff changeset
54 }
10003
3721ae3917fc Moved askpass() from master/ to lib/. Added t_askpass().
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
55 }
3721ae3917fc Moved askpass() from master/ to lib/. Added t_askpass().
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
56
12481
6ea1671108f1 t_askpass(): Removed a limit of 1024 bytes for the password.
Timo Sirainen <tss@iki.fi>
parents: 11039
diff changeset
57 void askpass(const char *prompt, char *buf, size_t buf_size)
6ea1671108f1 t_askpass(): Removed a limit of 1024 bytes for the password.
Timo Sirainen <tss@iki.fi>
parents: 11039
diff changeset
58 {
6ea1671108f1 t_askpass(): Removed a limit of 1024 bytes for the password.
Timo Sirainen <tss@iki.fi>
parents: 11039
diff changeset
59 buffer_t str;
6ea1671108f1 t_askpass(): Removed a limit of 1024 bytes for the password.
Timo Sirainen <tss@iki.fi>
parents: 11039
diff changeset
60
15034
7efef678bca8 Renamed buffer_create_*data() to buffer_create_from_*data() for consistency.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
61 buffer_create_from_data(&str, buf, buf_size);
12481
6ea1671108f1 t_askpass(): Removed a limit of 1024 bytes for the password.
Timo Sirainen <tss@iki.fi>
parents: 11039
diff changeset
62 askpass_str(prompt, &str);
6ea1671108f1 t_askpass(): Removed a limit of 1024 bytes for the password.
Timo Sirainen <tss@iki.fi>
parents: 11039
diff changeset
63 buffer_append_c(&str, '\0');
6ea1671108f1 t_askpass(): Removed a limit of 1024 bytes for the password.
Timo Sirainen <tss@iki.fi>
parents: 11039
diff changeset
64 }
6ea1671108f1 t_askpass(): Removed a limit of 1024 bytes for the password.
Timo Sirainen <tss@iki.fi>
parents: 11039
diff changeset
65
10003
3721ae3917fc Moved askpass() from master/ to lib/. Added t_askpass().
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
66 const char *t_askpass(const char *prompt)
3721ae3917fc Moved askpass() from master/ to lib/. Added t_askpass().
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
67 {
12481
6ea1671108f1 t_askpass(): Removed a limit of 1024 bytes for the password.
Timo Sirainen <tss@iki.fi>
parents: 11039
diff changeset
68 string_t *str = t_str_new(32);
10003
3721ae3917fc Moved askpass() from master/ to lib/. Added t_askpass().
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
69
12481
6ea1671108f1 t_askpass(): Removed a limit of 1024 bytes for the password.
Timo Sirainen <tss@iki.fi>
parents: 11039
diff changeset
70 askpass_str(prompt, str);
6ea1671108f1 t_askpass(): Removed a limit of 1024 bytes for the password.
Timo Sirainen <tss@iki.fi>
parents: 11039
diff changeset
71 return str_c(str);
10003
3721ae3917fc Moved askpass() from master/ to lib/. Added t_askpass().
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
72 }