summaryrefslogtreecommitdiffstats
path: root/imap/pop3login.c
diff options
context:
space:
mode:
Diffstat (limited to 'imap/pop3login.c')
-rw-r--r--imap/pop3login.c64
1 files changed, 60 insertions, 4 deletions
diff --git a/imap/pop3login.c b/imap/pop3login.c
index 4d43a62..05507ed 100644
--- a/imap/pop3login.c
+++ b/imap/pop3login.c
@@ -48,6 +48,63 @@ static const char *safe_getenv(const char *p)
return p;
}
+static char getline_buf[1024];
+static size_t getline_buf_size=0;
+static size_t getline_i=0;
+
+static int safe_getc()
+{
+ if (getline_i >= getline_buf_size)
+ {
+ int n=read(0, getline_buf, sizeof(getline_buf));
+
+ if (n < 0)
+ n=0;
+
+ if (n == 0)
+ return -1;
+
+ getline_buf_size=n;
+ getline_i=0;
+ }
+
+ return (int)(unsigned char)getline_buf[getline_i++];
+}
+
+static void safe_fflush()
+{
+ getline_i=getline_buf_size=0;
+}
+
+static char *safe_fgets(char *buf, size_t buf_size)
+{
+ size_t i;
+
+ for (i=0; i+1 < buf_size; ++i)
+ {
+ int ch=safe_getc();
+
+ if (ch < 0)
+ {
+ if (i == 0)
+ return NULL;
+ break;
+ }
+
+ buf[i]=ch;
+ if (ch == '\n')
+ {
+ ++i;
+ break;
+ }
+ }
+
+ if (i < buf_size)
+ buf[i]=0;
+
+ return buf;
+}
+
static int starttls()
{
char *argvec[4];
@@ -76,7 +133,7 @@ static int starttls()
printf("+OK Begin SSL/TLS negotiation now.\r\n");
fflush(stdout);
- fflush(stdin);
+ safe_fflush();
cinfo.username=MAILUSER;
if (couriertls_start(argvec, &cinfo))
@@ -101,7 +158,6 @@ static int starttls()
exit(1);
}
close(pipefd[0]);
- fflush(stdin);
putenv("POP3_STARTTLS=NO");
putenv("POP3_TLS_REQUIRED=0");
putenv("POP3_TLS=1");
@@ -116,7 +172,7 @@ char buf[BUFSIZ];
printf("+ %s\r\n", s);
fflush(stdout);
- if (fgets(buf, sizeof(buf), stdin) == 0) return (0);
+ if (safe_fgets(buf, sizeof(buf)) == 0) return (0);
if ((p=strchr(buf, '\n')) == 0) return (0);
if (p > buf && p[-1] == '\r') --p;
*p=0;
@@ -285,7 +341,7 @@ char *q ;
fflush(stdout);
fflush(stderr);
alarm(60);
- while (fgets(buf, sizeof(buf), stdin))
+ while (safe_fgets(buf, sizeof(buf)))
{
c=1;
for (p=buf; *p; p++)